4a153659197e6f16f130355a3ef3bb41809d8ff8
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();
27 S
::bootstrap('perms_backup', new PlFlagSet());
30 public function startAvailableAuth()
32 if (!(S
::v('perms') instanceof PlFlagSet
)) {
33 S
::set('perms', S
::v('perms_backup'));
36 $cookie = $this->tryCookie();
38 return $this->start(AUTH_COOKIE
);
39 } else if ($cookie == 1 ||
$cookie == -2) {
43 if ((check_ip('dangerous') && S
::has('uid')) ||
check_account()) {
44 $_SESSION['log']->log("view_page", $_SERVER['REQUEST_URI']);
49 /** Check the cookie and set the associated user_id in the auth_by_cookie session variable.
51 private function tryCookie()
53 S
::kill('auth_by_cookie');
54 if (Cookie
::v('ORGaccess') == '' ||
!Cookie
::has('ORGuid')) {
58 $res = XDB
::query('SELECT user_id, password
60 WHERE user_id = {?} AND perms IN(\'admin\', \'user\')',
62 if ($res->numRows() != 0) {
63 list($uid, $password) = $res->fetchOneRow();
64 require_once 'secure_hash.inc.php';
65 $expected_value = hash_encrypt($password);
66 if ($expected_value == Cookie
::v('ORGaccess')) {
67 S
::set('auth_by_cookie', $uid);
76 private function checkPassword($uname, $login, $response, $login_type)
78 $res = XDB
::query('SELECT u.user_id, u.password
79 FROM auth_user_md5 AS u
80 INNER JOIN aliases AS a ON (a.id = u.user_id AND type != \'homonyme\')
81 WHERE a.' . $login_type . ' = {?} AND u.perms IN(\'admin\', \'user\')',
83 if (list($uid, $password) = $res->fetchOneRow()) {
84 require_once 'secure_hash.inc.php';
85 $expected_response = hash_encrypt("$uname:$password:" . S
::v('challenge'));
86 if ($response != $expected_response) {
87 $new_password = hash_xor(Env
::v('xorpass'), $password);
88 $expected_response = hash_encrypt("$uname:$new_password:" . S
::v('challenge'));
89 if ($response == $expected_response) {
90 XDB
::execute('UPDATE auth_user_md5
96 if ($response != $expected_response) {
97 S
::logger($uid)->log('auth_fail', 'bad password');
108 protected function doAuth($level)
112 /* Cookie authentication
114 if ($level == AUTH_COOKIE
&& !S
::has('auth_by_cookie')) {
117 if ($level == AUTH_COOKIE
&& S
::has('auth_by_cookie')) {
119 S
::set('auth', AUTH_COOKIE
);
121 return S
::i('auth_by_cookie');
125 /* We want to do auth... we must have infos from a form.
127 if (!Post
::has('username') ||
!Post
::has('response') ||
!S
::has('challenge')) {
131 /** We come from an authentication form.
133 if (S
::has('suid')) {
134 $suid = S
::v('suid');
135 $login = $uname = $suid['forlife'];
138 $uname = Env
::v('username');
140 if (Env
::v('domain') == "alias") {
141 $res = XDB
::query('SELECT redirect
143 INNER JOIN virtual_redirect USING(vid)
144 WHERE alias LIKE {?}',
145 $uname . '@' . $globals->mail
->alias_dom
);
146 $redirect = $res->fetchOneCell();
148 $login = substr($redirect, 0, strpos($redirect, '@'));
158 $uid = $this->checkPassword($uname, $login, Post
::v('response'), (!$redirect && preg_match('/^\d*$/', $uname)) ?
'id' : 'alias');
159 if (!is_null($uid)) {
160 S
::set('auth', AUTH_MDP
);
161 if (Post
::has('domain')) {
162 if (($domain = Post
::v('domain', 'login')) == 'alias') {
163 setcookie('ORGdomain', "alias", (time() +
25920000), '/', '', 0);
165 setcookie('ORGdomain', '', (time() - 3600), '/', '', 0);
167 // pour que la modification soit effective dans le reste de la page
168 $_COOKIE['ORGdomain'] = $domain;
170 S
::kill('challenge');
171 S
::logger($uid)->log('auth_ok');
176 protected function startSessionAs($uid, $level)
178 if ((!is_null(S
::v('user')) && S
::i('user') != $uid) ||
(S
::has('uid') && S
::i('uid') != $uid)) {
180 } else if (S
::has('uid')) {
184 S
::set('auth', AUTH_COOKIE
);
186 unset($_SESSION['log']);
188 // Retrieves main user properties.
190 $res = XDB
::query("SELECT u.user_id AS uid, u.hruid, prenom, prenom_ini, nom, nom_ini, nom_usage, perms, promo, promo_sortie,
191 matricule, password, FIND_IN_SET('femme', u.flags) AS femme,
192 CONCAT(a.alias, '@{$globals->mail->domain}') AS forlife,
193 CONCAT(a2.alias, '@{$globals->mail->domain}') AS bestalias,
194 q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash,
195 FIND_IN_SET('watch', u.flags) AS watch_account, q.last_version, g.g_account_name IS NOT NULL AS googleapps
196 FROM auth_user_md5 AS u
197 INNER JOIN auth_user_quick AS q USING(user_id)
198 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = 'a_vie')
199 INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET('bestalias', a2.flags))
200 LEFT JOIN gapps_accounts AS g ON (u.user_id = g.l_userid AND g.g_status = 'active')
201 WHERE u.user_id = {?} AND u.perms IN('admin', 'user')", $uid);
202 $sess = $res->fetchOneAssoc();
203 $perms = $sess['perms'];
204 unset($sess['perms']);
206 // Retrieves account usage information (last login, last host).
207 $res = XDB
::query('SELECT UNIX_TIMESTAMP(s.start) AS lastlogin, s.host
208 FROM logger.sessions AS s
209 WHERE s.uid = {?} AND s.suid = 0
210 ORDER BY s.start DESC
212 if ($res->numRows()) {
213 $sess = array_merge($sess, $res->fetchOneAssoc());
216 // Loads the data into the real session.
217 $_SESSION = array_merge($_SESSION, $sess);
219 // Starts the session's logger, and sets up the permanent cookie.
220 if (S
::has('suid')) {
221 $suid = S
::v('suid');
222 $logger = S
::logger($uid);
223 $logger->log("suid_start", S
::v('hruid') . " by " . $suid['hruid']);
225 $logger = S
::logger($uid);
226 setcookie('ORGuid', $uid, (time() +
25920000), '/', '', 0);
227 if (Post
::v('remember', 'false') == 'true') {
228 $cookie = hash_encrypt($sess['password']);
229 setcookie('ORGaccess', $cookie, (time() +
25920000), '/', '', 0);
231 $logger->log("cookie_on");
234 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
236 $logger->log("cookie_off");
241 // Finalizes the session setup.
242 S
::set('perms', User
::makePerms($perms));
243 $this->securityChecks();
245 $this->updateNbNotifs();
250 private function securityChecks()
252 $mail_subject = array();
253 if (check_account()) {
254 $mail_subject[] = 'Connexion d\'un utilisateur surveillé';
256 if (check_ip('unsafe')) {
257 $mail_subject[] = 'Une IP surveillee a tente de se connecter';
258 if (check_ip('ban')) {
259 send_warning_mail(implode(' - ', $mail_subject));
261 Platal
::page()->kill('Une erreur est survenue lors de la procédure d\'authentification. '
262 . 'Merci de contacter au plus vite '
263 . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>');
267 if (count($mail_subject)) {
268 send_warning_mail(implode(' - ', $mail_subject));
272 public function tokenAuth($login, $token)
274 $res = XDB
::query('SELECT u.hruid
276 INNER JOIN auth_user_md5 AS u ON (a.id = u.user_id AND u.perms IN ("admin", "user"))
277 INNER JOIN auth_user_quick AS q ON (a.id = q.user_id AND q.core_rss_hash = {?})
278 WHERE a.alias = {?} AND a.type != "homonyme"', $token, $login);
279 if ($res->numRows() == 1) {
280 $data = $res->fetchOneAssoc();
281 return new User($data['hruid'], $data);
286 public function setSkin()
289 if (S
::logged() && (!S
::has('skin') || S
::has('suid'))) {
291 $res = XDB
::query("SELECT skin_tpl
292 FROM auth_user_quick AS a
293 INNER JOIN skins AS s ON a.skin = s.id
294 WHERE user_id = {?} AND skin_tpl != ''", $uid);
295 S
::set('skin', $res->fetchOneCell());
299 public function sureLevel()
305 public function updateNbNotifs()
307 require_once 'notifs.inc.php';
308 $n = select_notifs(false
, S
::i('uid'), S
::v('watch_last'), false
);
309 S
::set('notifs', $n->numRows());
313 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: