004b8c5ff67fbb9de2a87ac47b78df1232ec4771
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 public function __construct()
26 parent
::__construct();
29 public function startAvailableAuth()
32 $cookie = $this->tryCookie();
34 return $this->start(AUTH_COOKIE
);
35 } else if ($cookie == 1 ||
$cookie == -2) {
39 if ((check_ip('dangerous') && S
::has('uid')) ||
check_account()) {
40 S
::logger()->log("view_page", $_SERVER['REQUEST_URI']);
45 /** Check the cookie and set the associated user_id in the auth_by_cookie session variable.
47 private function tryCookie()
49 S
::kill('auth_by_cookie');
50 if (Cookie
::v('access') == '' ||
!Cookie
::has('uid')) {
54 $res = XDB
::query('SELECT uid, password
56 WHERE uid = {?} AND state = \'active\'',
58 if ($res->numRows() != 0) {
59 list($uid, $password) = $res->fetchOneRow();
60 $expected_value = sha1($password);
61 if ($expected_value == Cookie
::v('access')) {
62 S
::set('auth_by_cookie', $uid);
71 private function checkPassword($uname, $login, $response, $login_type)
73 $res = XDB
::query('SELECT a.uid, a.password
75 INNER JOIN aliases AS l ON (l.id = a.uid AND l.type != \'homonyme\')
76 WHERE l.' . $login_type . ' = {?} AND a.state = \'active\'',
78 if (list($uid, $password) = $res->fetchOneRow()) {
79 $expected_response = sha1("$uname:$password:" . S
::v('challenge'));
80 /* XXX: Deprecates len(password) > 10 conversion */
81 if ($response != $expected_response) {
83 Platal
::page()->trigError('Mot de passe ou nom d\'utilisateur invalide');
85 Platal
::page()->trigError('Mot de passe invalide');
87 S
::logger($uid)->log('auth_fail', 'bad password');
92 Platal
::page()->trigError('Mot de passe ou nom d\'utilisateur invalide');
99 protected function doAuth($level)
103 /* Cookie authentication
105 if ($level == AUTH_COOKIE
&& !S
::has('auth_by_cookie')) {
108 if ($level == AUTH_COOKIE
&& S
::has('auth_by_cookie')) {
110 S
::set('auth', AUTH_COOKIE
);
112 return User
::getSilentWithValues(null
, array('user_id' => S
::i('auth_by_cookie')));
116 /* We want to do auth... we must have infos from a form.
118 if (!Post
::has('username') ||
!Post
::has('response') ||
!S
::has('challenge')) {
122 /** We come from an authentication form.
125 $login = $uname = S
::suid('uid');
128 $uname = Env
::v('username');
129 if (Env
::v('domain') == "alias") {
130 $res = XDB
::query('SELECT redirect
132 INNER JOIN virtual_redirect USING(vid)
133 WHERE alias LIKE {?}',
134 $uname . '@' . $globals->mail
->alias_dom
);
135 $redirect = $res->fetchOneCell();
137 $login = substr($redirect, 0, strpos($redirect, '@'));
147 $uid = $this->checkPassword($uname, $login, Post
::v('response'), (!$redirect && is_numeric($uname)) ?
'id' : 'alias');
148 if (!is_null($uid) && S
::suid()) {
149 if (S
::suid('uid') == $uid) {
155 if (!is_null($uid)) {
156 S
::set('auth', AUTH_MDP
);
158 if (Post
::has('domain')) {
159 if (($domain = Post
::v('domain', 'login')) == 'alias') {
160 Cookie
::set('domain', 'alias', 300);
162 Cookie
::kill('domain');
166 S
::kill('challenge');
167 S
::logger($uid)->log('auth_ok');
169 return User
::getSilentWithValues(null
, array('user_id' => $uid));
172 protected function startSessionAs($user, $level)
174 if ((!is_null(S
::v('user')) && S
::v('user')->id() != $user->id())
175 ||
(S
::has('uid') && S
::i('uid') != $user->id())) {
177 } else if (S
::has('uid')) {
180 if ($level == AUTH_SUID
) {
181 S
::set('auth', AUTH_MDP
);
184 // Retrieves main user properties.
185 /** TODO: Move needed informations to account tables */
186 /** TODO: Currently suppressed data are matricule, promo */
187 /** TODO: Data to move are: banana_last, watch_last, last_version */
188 /** TODO: Use the User object to fetch all this */
189 $res = XDB
::query("SELECT a.uid, a.hruid, a.display_name, a.full_name, a.password,
190 a.sex = 'female' AS femme, a.email_format,
191 a.token, FIND_IN_SET('watch', a.flags) AS watch_account,
192 UNIX_TIMESTAMP(fp.last_seen) AS banana_last, q.watch_last,
193 q.last_version, g.g_account_name IS NOT NULL AS googleapps,
194 UNIX_TIMESTAMP(s.start) AS lastlogin, s.host,
197 INNER JOIN account_types AS at ON(a.type = at.type)
198 INNER JOIN auth_user_quick AS q ON(a.uid = q.user_id)
199 LEFT JOIN forum_profiles AS fp ON(fp.uid = a.uid)
200 LEFT JOIN gapps_accounts AS g ON(a.uid = g.l_userid AND g.g_status = 'active')
201 LEFT JOIN logger.last_sessions AS ls ON (ls.uid = a.uid)
202 LEFT JOIN logger.sessions AS s ON(s.id = ls.id)
203 WHERE a.uid = {?} AND a.state = 'active'", $user->id());
204 $sess = $res->fetchOneAssoc();
205 $perms = $sess['perms'];
206 unset($sess['perms']);
208 // Loads the data into the real session.
209 $_SESSION = array_merge($_SESSION, $sess);
211 // Starts the session's logger, and sets up the permanent cookie.
213 S
::logger()->log("suid_start", S
::v('hruid') . ' by ' . S
::suid('hruid'));
215 S
::logger()->saveLastSession();
216 Cookie
::set('uid', $user->id(), 300);
218 if (S
::i('auth_by_cookie') == $user->id() || Post
::v('remember', 'false') == 'true') {
219 $this->setAccessCookie(false
, S
::i('auth_by_cookie') != $user->id());
221 $this->killAccessCookie();
225 // Finalizes the session setup.
226 $this->makePerms($perms, S
::b('is_admin'));
227 $this->securityChecks();
229 $this->updateNbNotifs();
232 // We should not have to use this private data anymore
233 S
::kill('auth_by_cookie');
237 private function securityChecks()
239 $mail_subject = array();
240 if (check_account()) {
241 $mail_subject[] = 'Connexion d\'un utilisateur surveillé';
243 if (check_ip('unsafe')) {
244 $mail_subject[] = 'Une IP surveillee a tente de se connecter';
245 if (check_ip('ban')) {
246 send_warning_mail(implode(' - ', $mail_subject));
248 Platal
::page()->kill('Une erreur est survenue lors de la procédure d\'authentification. '
249 . 'Merci de contacter au plus vite '
250 . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>');
254 if (count($mail_subject)) {
255 send_warning_mail(implode(' - ', $mail_subject));
259 public function tokenAuth($login, $token)
261 $res = XDB
::query('SELECT a.uid AS user_id, a.hruid
263 INNER JOIN accounts AS a ON (l.id = a.uid AND a.state = \'active\')
264 WHERE a.token = {?} AND l.alias = {?} AND l.type != \'homonyme\'',
266 if ($res->numRows() == 1) {
267 return new User(null
, $res->fetchOneAssoc());
272 protected function makePerms($perm, $is_admin)
274 S
::set('perms', User
::makePerms($perm, $is_admin));
277 public function setSkin()
279 if (S
::logged() && (!S
::has('skin') || S
::suid())) {
281 $res = XDB
::query('SELECT skin_tpl
283 INNER JOIN skins AS s on (a.skin = s.id)
284 WHERE a.uid = {?} AND skin_tpl != \'\'', S
::i('uid'));
285 S
::set('skin', $res->fetchOneCell());
289 public function loggedLevel()
294 public function sureLevel()
300 public function updateNbNotifs()
302 require_once 'notifs.inc.php';
303 $n = select_notifs(false
, S
::i('uid'), S
::v('watch_last'), false
);
304 S
::set('notifs', $n->numRows());
307 public function setAccessCookie($replace = false
, $log = true
) {
308 if (S
::suid() ||
($replace && !Cookie
::blank('access'))) {
311 Cookie
::set('access', sha1(S
::v('password')), 300, true
);
313 S
::logger()->log('cookie_on');
317 public function killAccessCookie($log = true
) {
318 Cookie
::kill('access');
320 S
::logger()->log('cookie_off');
324 public function killLoginFormCookies() {
326 Cookie
::kill('domain');
330 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: