Commit | Line | Data |
---|---|---|
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 | 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()) { |
9d214e8e | 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 | |
c67ba12a FB |
54 | $res = XDB::query('SELECT uid, password |
55 | FROM accounts | |
56 | WHERE uid = {?} AND state = \'active\'', | |
8cd3dccc | 57 | Cookie::i('uid')); |
c0799142 FB |
58 | if ($res->numRows() != 0) { |
59 | list($uid, $password) = $res->fetchOneRow(); | |
c67ba12a | 60 | $expected_value = sha1($password); |
40565fa2 | 61 | if ($expected_value == Cookie::v('access')) { |
c0799142 FB |
62 | S::set('auth_by_cookie', $uid); |
63 | return 0; | |
64 | } else { | |
65 | return 1; | |
66 | } | |
67 | } | |
68 | return -2; | |
69 | } | |
70 | ||
71 | private function checkPassword($uname, $login, $response, $login_type) | |
5480a216 | 72 | { |
c67ba12a FB |
73 | $res = XDB::query('SELECT a.uid, a.password |
74 | FROM accounts AS a | |
75 | INNER JOIN aliases AS l ON (l.id = a.uid AND l.type != \'homonyme\') | |
76 | WHERE l.' . $login_type . ' = {?} AND a.state = \'active\'', | |
c0799142 FB |
77 | $login); |
78 | if (list($uid, $password) = $res->fetchOneRow()) { | |
c67ba12a FB |
79 | $expected_response = sha1("$uname:$password:" . S::v('challenge')); |
80 | /* XXX: Deprecates len(password) > 10 conversion */ | |
c0799142 | 81 | if ($response != $expected_response) { |
86777849 FB |
82 | if (!S::logged()) { |
83 | Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide'); | |
84 | } else { | |
85 | Platal::page()->trigError('Mot de passe invalide'); | |
86 | } | |
732e5855 | 87 | S::logger($uid)->log('auth_fail', 'bad password'); |
c0799142 FB |
88 | return null; |
89 | } | |
90 | return $uid; | |
91 | } | |
c6bad0e7 | 92 | Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide'); |
c0799142 | 93 | return null; |
0337d704 | 94 | } |
cab08090 | 95 | |
0337d704 | 96 | |
c0799142 FB |
97 | /** Check auth. |
98 | */ | |
99 | protected function doAuth($level) | |
0337d704 | 100 | { |
e74411f7 | 101 | global $globals; |
c0799142 FB |
102 | |
103 | /* Cookie authentication | |
104 | */ | |
105 | if ($level == AUTH_COOKIE && !S::has('auth_by_cookie')) { | |
106 | $this->tryCookie(); | |
107 | } | |
108 | if ($level == AUTH_COOKIE && S::has('auth_by_cookie')) { | |
109 | if (!S::logged()) { | |
110 | S::set('auth', AUTH_COOKIE); | |
111 | } | |
866bd535 | 112 | return User::getSilentWithValues(null, array('user_id' => S::i('auth_by_cookie'))); |
e74411f7 | 113 | } |
0337d704 | 114 | |
c0799142 FB |
115 | |
116 | /* We want to do auth... we must have infos from a form. | |
117 | */ | |
e64c8b61 | 118 | if (!Post::has('username') || !Post::has('response') || !S::has('challenge')) { |
c0799142 | 119 | return null; |
63528107 | 120 | } |
121 | ||
c0799142 FB |
122 | /** We come from an authentication form. |
123 | */ | |
0c02607e FB |
124 | if (S::suid()) { |
125 | $login = $uname = S::suid('uid'); | |
e74411f7 | 126 | $redirect = false; |
127 | } else { | |
128 | $uname = Env::v('username'); | |
e74411f7 | 129 | if (Env::v('domain') == "alias") { |
c0799142 FB |
130 | $res = XDB::query('SELECT redirect |
131 | FROM virtual | |
132 | INNER JOIN virtual_redirect USING(vid) | |
133 | WHERE alias LIKE {?}', | |
134 | $uname . '@' . $globals->mail->alias_dom); | |
e74411f7 | 135 | $redirect = $res->fetchOneCell(); |
136 | if ($redirect) { | |
137 | $login = substr($redirect, 0, strpos($redirect, '@')); | |
138 | } else { | |
c0799142 | 139 | $login = ''; |
e74411f7 | 140 | } |
0337d704 | 141 | } else { |
e74411f7 | 142 | $login = $uname; |
143 | $redirect = false; | |
0337d704 | 144 | } |
63528107 | 145 | } |
cab08090 | 146 | |
888465dd | 147 | $uid = $this->checkPassword($uname, $login, Post::v('response'), (!$redirect && is_numeric($uname)) ? 'id' : 'alias'); |
0c02607e FB |
148 | if (!is_null($uid) && S::suid()) { |
149 | if (S::suid('uid') == $uid) { | |
888465dd FB |
150 | $uid = S::i('uid'); |
151 | } else { | |
152 | $uid = null; | |
153 | } | |
154 | } | |
c0799142 FB |
155 | if (!is_null($uid)) { |
156 | S::set('auth', AUTH_MDP); | |
0c02607e | 157 | if (!S::suid()) { |
888465dd FB |
158 | if (Post::has('domain')) { |
159 | if (($domain = Post::v('domain', 'login')) == 'alias') { | |
40565fa2 | 160 | Cookie::set('domain', 'alias', 300); |
888465dd | 161 | } else { |
40565fa2 | 162 | Cookie::kill('domain'); |
888465dd | 163 | } |
e74411f7 | 164 | } |
165 | } | |
c0799142 | 166 | S::kill('challenge'); |
732e5855 | 167 | S::logger($uid)->log('auth_ok'); |
c0799142 | 168 | } |
e2cb093d | 169 | return User::getSilentWithValues(null, array('user_id' => $uid)); |
c0799142 | 170 | } |
cab08090 | 171 | |
e2cb093d | 172 | protected function startSessionAs($user, $level) |
c0799142 | 173 | { |
867b04ef | 174 | if ((!is_null(S::v('user')) && S::v('user')->id() != $user->id()) |
e2cb093d | 175 | || (S::has('uid') && S::i('uid') != $user->id())) { |
c0799142 FB |
176 | return false; |
177 | } else if (S::has('uid')) { | |
178 | return true; | |
179 | } | |
888465dd FB |
180 | if ($level == AUTH_SUID) { |
181 | S::set('auth', AUTH_MDP); | |
c0799142 | 182 | } |
70232020 | 183 | |
6672b29b | 184 | // Retrieves main user properties. |
4182c6b5 FB |
185 | /** TODO: Move needed informations to account tables */ |
186 | /** TODO: Currently suppressed data are matricule, promo */ | |
e2cb093d | 187 | /** TODO: Use the User object to fetch all this */ |
4182c6b5 | 188 | $res = XDB::query("SELECT a.uid, a.hruid, a.display_name, a.full_name, a.password, |
867b04ef | 189 | a.sex = 'female' AS femme, a.email_format, |
4182c6b5 | 190 | a.token, FIND_IN_SET('watch', a.flags) AS watch_account, |
0511895d FB |
191 | UNIX_TIMESTAMP(fp.last_seen) AS banana_last, w.last AS watch_last, |
192 | a.last_version, g.g_account_name IS NOT NULL AS googleapps, | |
4182c6b5 | 193 | UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, |
365ba8c3 | 194 | a.is_admin, at.perms |
4182c6b5 | 195 | FROM accounts AS a |
365ba8c3 | 196 | INNER JOIN account_types AS at ON(a.type = at.type) |
0511895d | 197 | INNER JOIN watch AS w ON(w.uid = a.uid) |
7d15f750 | 198 | LEFT JOIN forum_profiles AS fp ON(fp.uid = a.uid) |
4182c6b5 FB |
199 | LEFT JOIN gapps_accounts AS g ON(a.uid = g.l_userid AND g.g_status = 'active') |
200 | LEFT JOIN logger.last_sessions AS ls ON (ls.uid = a.uid) | |
6a61c46d | 201 | LEFT JOIN logger.sessions AS s ON(s.id = ls.id) |
e2cb093d | 202 | WHERE a.uid = {?} AND a.state = 'active'", $user->id()); |
c0799142 FB |
203 | $sess = $res->fetchOneAssoc(); |
204 | $perms = $sess['perms']; | |
205 | unset($sess['perms']); | |
6672b29b | 206 | |
6672b29b VZ |
207 | // Loads the data into the real session. |
208 | $_SESSION = array_merge($_SESSION, $sess); | |
209 | ||
210 | // Starts the session's logger, and sets up the permanent cookie. | |
0c02607e FB |
211 | if (S::suid()) { |
212 | S::logger()->log("suid_start", S::v('hruid') . ' by ' . S::suid('hruid')); | |
c0799142 | 213 | } else { |
e2cb093d FB |
214 | S::logger()->saveLastSession(); |
215 | Cookie::set('uid', $user->id(), 300); | |
a90cde48 | 216 | |
e2cb093d FB |
217 | if (S::i('auth_by_cookie') == $user->id() || Post::v('remember', 'false') == 'true') { |
218 | $this->setAccessCookie(false, S::i('auth_by_cookie') != $user->id()); | |
c0799142 | 219 | } else { |
604dfd58 | 220 | $this->killAccessCookie(); |
0337d704 | 221 | } |
63528107 | 222 | } |
b0b937fd | 223 | |
6672b29b | 224 | // Finalizes the session setup. |
365ba8c3 | 225 | $this->makePerms($perms, S::b('is_admin')); |
c0799142 FB |
226 | $this->securityChecks(); |
227 | $this->setSkin(); | |
ebfdf077 | 228 | $this->updateNbNotifs(); |
c0799142 | 229 | check_redirect(); |
d0621f88 FB |
230 | |
231 | // We should not have to use this private data anymore | |
232 | S::kill('auth_by_cookie'); | |
c0799142 | 233 | return true; |
0337d704 | 234 | } |
235 | ||
c0799142 | 236 | private function securityChecks() |
0337d704 | 237 | { |
c0799142 FB |
238 | $mail_subject = array(); |
239 | if (check_account()) { | |
240 | $mail_subject[] = 'Connexion d\'un utilisateur surveillé'; | |
0337d704 | 241 | } |
c0799142 FB |
242 | if (check_ip('unsafe')) { |
243 | $mail_subject[] = 'Une IP surveillee a tente de se connecter'; | |
244 | if (check_ip('ban')) { | |
245 | send_warning_mail(implode(' - ', $mail_subject)); | |
246 | $this->destroy(); | |
247 | Platal::page()->kill('Une erreur est survenue lors de la procédure d\'authentification. ' | |
248 | . 'Merci de contacter au plus vite ' | |
249 | . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>'); | |
250 | return false; | |
251 | } | |
0337d704 | 252 | } |
c0799142 FB |
253 | if (count($mail_subject)) { |
254 | send_warning_mail(implode(' - ', $mail_subject)); | |
0337d704 | 255 | } |
256 | } | |
257 | ||
0d44ce42 FB |
258 | public function tokenAuth($login, $token) |
259 | { | |
8c12f931 | 260 | $res = XDB::query('SELECT a.uid AS user_id, a.hruid |
c67ba12a FB |
261 | FROM aliases AS l |
262 | INNER JOIN accounts AS a ON (l.id = a.uid AND a.state = \'active\') | |
263 | WHERE a.token = {?} AND l.alias = {?} AND l.type != \'homonyme\'', | |
264 | $token, $login); | |
0d44ce42 | 265 | if ($res->numRows() == 1) { |
8c12f931 | 266 | return new User(null, $res->fetchOneAssoc()); |
0d44ce42 FB |
267 | } |
268 | return null; | |
269 | } | |
270 | ||
365ba8c3 | 271 | protected function makePerms($perm, $is_admin) |
bf517daf | 272 | { |
365ba8c3 | 273 | S::set('perms', User::makePerms($perm, $is_admin)); |
bf517daf | 274 | } |
275 | ||
c0799142 FB |
276 | public function setSkin() |
277 | { | |
0c02607e | 278 | if (S::logged() && (!S::has('skin') || S::suid())) { |
c67ba12a FB |
279 | $res = XDB::query('SELECT skin_tpl |
280 | FROM accounts AS a | |
281 | INNER JOIN skins AS s on (a.skin = s.id) | |
282 | WHERE a.uid = {?} AND skin_tpl != \'\'', S::i('uid')); | |
c0799142 | 283 | S::set('skin', $res->fetchOneCell()); |
5480a216 | 284 | } |
285 | } | |
0337d704 | 286 | |
faa06583 FB |
287 | public function loggedLevel() |
288 | { | |
289 | return AUTH_COOKIE; | |
290 | } | |
291 | ||
c0799142 FB |
292 | public function sureLevel() |
293 | { | |
294 | return AUTH_MDP; | |
0337d704 | 295 | } |
ebfdf077 FB |
296 | |
297 | ||
298 | public function updateNbNotifs() | |
299 | { | |
300 | require_once 'notifs.inc.php'; | |
301 | $n = select_notifs(false, S::i('uid'), S::v('watch_last'), false); | |
302 | S::set('notifs', $n->numRows()); | |
303 | } | |
604dfd58 FB |
304 | |
305 | public function setAccessCookie($replace = false, $log = true) { | |
0c02607e | 306 | if (S::suid() || ($replace && !Cookie::blank('access'))) { |
604dfd58 FB |
307 | return; |
308 | } | |
55d55de8 | 309 | Cookie::set('access', sha1(S::v('password')), 300, true); |
604dfd58 FB |
310 | if ($log) { |
311 | S::logger()->log('cookie_on'); | |
312 | } | |
313 | } | |
314 | ||
315 | public function killAccessCookie($log = true) { | |
316 | Cookie::kill('access'); | |
317 | if ($log) { | |
318 | S::logger()->log('cookie_off'); | |
319 | } | |
320 | } | |
321 | ||
322 | public function killLoginFormCookies() { | |
323 | Cookie::kill('uid'); | |
324 | Cookie::kill('domain'); | |
325 | } | |
0337d704 | 326 | } |
327 | ||
a7de4ef7 | 328 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 329 | ?> |