c7a5afdfe557fedb25c8eecc06f381b91e02d4c2
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class XorgSession
extends PlSession
24 const INVALID_USER
= -2;
26 const COOKIE_SUCCESS
= 0;
27 const INVALID_COOKIE
= 1;
29 public function __construct()
31 parent
::__construct();
34 public function startAvailableAuth()
37 switch ($this->tryCookie()) {
38 case self
::COOKIE_SUCCESS
:
39 if (!$this->start(AUTH_COOKIE
)) {
44 case self
::INVALID_USER
:
45 case self
::INVALID_COOKIE
:
49 if ((check_ip('dangerous') && S
::has('uid')) ||
check_account()) {
50 S
::logger()->log("view_page", $_SERVER['REQUEST_URI']);
55 /** Check the cookie and set the associated uid in the auth_by_cookie session variable.
57 private function tryCookie()
59 S
::kill('auth_by_cookie');
60 if (Cookie
::v('access') == '' ||
!Cookie
::has('uid')) {
61 return self
::NO_COOKIE
;
64 $res = XDB
::query('SELECT uid, password
66 WHERE uid = {?} AND state = \'active\'',
68 if ($res->numRows() != 0) {
69 list($uid, $password) = $res->fetchOneRow();
70 if (sha1($password) == Cookie
::v('access')) {
71 S
::set('auth_by_cookie', $uid);
72 return self
::COOKIE_SUCCESS
;
74 return self
::INVALID_COOKIE
;
77 return self
::INVALID_USER
;
80 private function checkPassword($uname, $login, $response, $login_type)
82 if ($login_type == 'alias') {
83 $res = XDB
::query('SELECT a.uid, a.password
85 INNER JOIN aliases AS l ON (l.uid = a.uid AND l.type != \'homonyme\')
86 WHERE l.alias = {?} AND a.state = \'active\'',
89 $res = XDB
::query('SELECT uid, password
91 WHERE ' . $login_type . ' = {?}',
94 if (list($uid, $password) = $res->fetchOneRow()) {
95 $expected_response = sha1("$uname:$password:" . S
::v('challenge'));
96 /* Deprecates len(password) > 10 conversion. */
97 if ($response != $expected_response) {
99 Platal
::page()->trigError('Mot de passe ou nom d\'utilisateur invalide');
101 Platal
::page()->trigError('Mot de passe invalide');
103 S
::logger($uid)->log('auth_fail', 'bad password');
108 Platal
::page()->trigError('Mot de passe ou nom d\'utilisateur invalide');
115 protected function doAuth($level)
119 /* Cookie authentication
121 if ($level == AUTH_COOKIE
&& !S
::has('auth_by_cookie')) {
124 if ($level == AUTH_COOKIE
&& S
::has('auth_by_cookie')) {
126 S
::set('auth', AUTH_COOKIE
);
128 return User
::getSilentWithUID(S
::i('auth_by_cookie'));
132 /* We want to do auth... we must have infos from a form.
134 if (!Post
::has('username') ||
!Post
::has('response') ||
!S
::has('challenge')) {
138 /** We come from an authentication form.
141 $login = $uname = S
::suid('uid');
145 $uname = Post
::v('username');
146 if (Post
::s('domain') == "alias") {
147 $res = XDB
::query('SELECT redirect
149 INNER JOIN virtual_redirect USING(vid)
150 WHERE alias LIKE {?}',
151 $uname . '@' . $globals->mail
->alias_dom
);
152 $redirect = $res->fetchOneCell();
154 $login = substr($redirect, 0, strpos($redirect, '@'));
158 $loginType = 'alias';
159 } else if (Post
::s('domain') == "ax") {
162 $loginType = 'hruid';
166 $loginType = is_numeric($uname) ?
'uid' : 'alias';
170 $uid = $this->checkPassword($uname, $login, Post
::v('response'), $loginType);
171 if (!is_null($uid) && S
::suid()) {
172 if (S
::suid('uid') == $uid) {
178 if (!is_null($uid)) {
179 S
::set('auth', AUTH_MDP
);
181 if (Post
::has('domain')) {
182 $domain = Post
::v('domain', 'login');
183 if ($domain == 'alias') {
184 Cookie
::set('domain', 'alias', 300);
185 } else if ($domain == 'ax') {
186 Cookie
::set('domain', 'ax', 300);
188 Cookie
::kill('domain');
192 S
::kill('challenge');
193 S
::logger($uid)->log('auth_ok');
195 return User
::getSilentWithUID($uid);
198 protected function startSessionAs($user, $level)
200 if ((!is_null(S
::user()) && S
::user()->id() != $user->id())
201 ||
(S
::has('uid') && S
::i('uid') != $user->id())) {
203 } else if (S
::has('uid')) {
206 if ($level == AUTH_SUID
) {
207 S
::set('auth', AUTH_MDP
);
210 // Loads uid and hruid into the session for developement conveniance.
211 $_SESSION = array_merge($_SESSION, array('uid' => $user->id(), 'hruid' => $user->hruid
, 'token' => $user->token
));
213 // Starts the session's logger, and sets up the permanent cookie.
215 S
::logger()->log("suid_start", S
::v('hruid') . ' by ' . S
::suid('hruid'));
217 S
::logger()->saveLastSession();
218 Cookie
::set('uid', $user->id(), 300);
220 if (S
::i('auth_by_cookie') == $user->id() || Post
::v('remember', 'false') == 'true') {
221 $this->setAccessCookie(false
, S
::i('auth_by_cookie') != $user->id());
223 $this->killAccessCookie();
227 // Finalizes the session setup.
228 $this->makePerms($user->perms
, $user->is_admin
);
229 $this->securityChecks();
231 $this->updateNbNotifs();
234 // We should not have to use this private data anymore
235 S
::kill('auth_by_cookie');
239 private function securityChecks()
241 $mail_subject = array();
242 if (check_account()) {
243 $mail_subject[] = 'Connexion d\'un utilisateur surveillé';
245 if (check_ip('unsafe')) {
246 $mail_subject[] = 'Une IP surveillee a tente de se connecter';
247 if (check_ip('ban')) {
248 send_warning_mail(implode(' - ', $mail_subject));
250 Platal
::page()->kill('Une erreur est survenue lors de la procédure d\'authentification. '
251 . 'Merci de contacter au plus vite '
252 . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>');
256 if (count($mail_subject)) {
257 send_warning_mail(implode(' - ', $mail_subject));
261 public function tokenAuth($login, $token)
263 $res = XDB
::query('SELECT a.uid, a.hruid
265 INNER JOIN accounts AS a ON (l.uid = a.uid AND a.state = \'active\')
266 WHERE a.token = {?} AND l.alias = {?} AND l.type != \'homonyme\'',
268 if ($res->numRows() == 1) {
269 return new User(null
, $res->fetchOneAssoc());
274 protected function makePerms($perm, $is_admin)
276 S
::set('perms', User
::makePerms($perm, $is_admin));
279 public function setSkin()
281 if (S
::logged() && (!S
::has('skin') || S
::suid())) {
282 $res = XDB
::query('SELECT skin_tpl
284 INNER JOIN skins AS s on (a.skin = s.id)
285 WHERE a.uid = {?} AND skin_tpl != \'\'', S
::i('uid'));
286 S
::set('skin', $res->fetchOneCell());
290 public function loggedLevel()
295 public function sureLevel()
301 public function updateNbNotifs()
303 require_once 'notifs.inc.php';
305 $n = Watch
::getCount($user);
306 S
::set('notifs', $n);
309 public function setAccessCookie($replace = false
, $log = true
) {
310 if (S
::suid() ||
($replace && !Cookie
::blank('access'))) {
313 Cookie
::set('access', sha1(S
::user()->password()), 300, true
);
315 S
::logger()->log('cookie_on');
319 public function killAccessCookie($log = true
) {
320 Cookie
::kill('access');
322 S
::logger()->log('cookie_off');
326 public function killLoginFormCookies() {
328 Cookie
::kill('domain');
332 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: