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 $_SESSION['log']->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('ORGaccess') == '' ||
!Cookie
::has('ORGuid')) {
54 $res = XDB
::query('SELECT user_id, password
56 WHERE user_id = {?} AND perms IN(\'admin\', \'user\')',
58 if ($res->numRows() != 0) {
59 list($uid, $password) = $res->fetchOneRow();
60 require_once 'secure_hash.inc.php';
61 $expected_value = hash_encrypt($password);
62 if ($expected_value == Cookie
::v('ORGaccess')) {
63 S
::set('auth_by_cookie', $uid);
72 private function checkPassword($uname, $login, $response, $login_type)
74 $res = XDB
::query('SELECT u.user_id, u.password
75 FROM auth_user_md5 AS u
76 INNER JOIN aliases AS a ON (a.id = u.user_id AND type != \'homonyme\')
77 WHERE a.' . $login_type . ' = {?} AND u.perms IN(\'admin\', \'user\')',
79 if (list($uid, $password) = $res->fetchOneRow()) {
80 require_once 'secure_hash.inc.php';
81 $expected_response = hash_encrypt("$uname:$password:" . S
::v('challenge'));
82 if ($response != $expected_response && Env
::has('xorpass')
83 && !preg_match('/^0*$/', Env
::v('xorpass'))) {
84 $new_password = hash_xor(Env
::v('xorpass'), $password);
85 $expected_response = hash_encrypt("$uname:$new_password:" . S
::v('challenge'));
86 if ($response == $expected_response) {
87 XDB
::execute('UPDATE auth_user_md5
92 // Update the GoogleApps password as well, if required.
94 if ($globals->mailstorage
->googleapps_domain
) {
95 require_once 'googleapps.inc.php';
96 $user = User
::getSilent($uid);
97 $account = new GoogleAppsAccount($user);
98 if ($account->active() && $account->sync_password
) {
99 $account->set_password($new_password);
104 if ($response != $expected_response) {
105 S
::logger($uid)->log('auth_fail', 'bad password');
116 protected function doAuth($level)
120 /* Cookie authentication
122 if ($level == AUTH_COOKIE
&& !S
::has('auth_by_cookie')) {
125 if ($level == AUTH_COOKIE
&& S
::has('auth_by_cookie')) {
127 S
::set('auth', AUTH_COOKIE
);
129 return S
::i('auth_by_cookie');
133 /* We want to do auth... we must have infos from a form.
135 if (!Post
::has('username') ||
!Post
::has('response') ||
!S
::has('challenge')) {
139 /** We come from an authentication form.
141 if (S
::has('suid')) {
142 $suid = S
::v('suid');
143 $login = $uname = $suid['hruid'];
146 $uname = Env
::v('username');
148 if (Env
::v('domain') == "alias") {
149 $res = XDB
::query('SELECT redirect
151 INNER JOIN virtual_redirect USING(vid)
152 WHERE alias LIKE {?}',
153 $uname . '@' . $globals->mail
->alias_dom
);
154 $redirect = $res->fetchOneCell();
156 $login = substr($redirect, 0, strpos($redirect, '@'));
166 $uid = $this->checkPassword($uname, $login, Post
::v('response'), (!$redirect && preg_match('/^\d*$/', $uname)) ?
'id' : 'alias');
167 if (!is_null($uid)) {
168 S
::set('auth', AUTH_MDP
);
169 if (Post
::has('domain')) {
170 if (($domain = Post
::v('domain', 'login')) == 'alias') {
171 setcookie('ORGdomain', "alias", (time() +
25920000), '/', '', 0);
173 setcookie('ORGdomain', '', (time() - 3600), '/', '', 0);
175 // pour que la modification soit effective dans le reste de la page
176 $_COOKIE['ORGdomain'] = $domain;
178 S
::kill('challenge');
179 S
::logger($uid)->log('auth_ok');
184 protected function startSessionAs($uid, $level)
186 if ((!is_null(S
::v('user')) && S
::i('user') != $uid) ||
(S
::has('uid') && S
::i('uid') != $uid)) {
188 } else if (S
::has('uid')) {
192 S
::set('auth', AUTH_COOKIE
);
194 unset($_SESSION['log']);
196 // Retrieves main user properties.
197 $res = XDB
::query("SELECT u.user_id AS uid, u.hruid, prenom, prenom_ini, nom, nom_ini, nom_usage, perms, promo, promo_sortie,
198 matricule, password, FIND_IN_SET('femme', u.flags) AS femme,
199 q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash,
200 FIND_IN_SET('watch', u.flags) AS watch_account, q.last_version, g.g_account_name IS NOT NULL AS googleapps
201 FROM auth_user_md5 AS u
202 INNER JOIN auth_user_quick AS q USING(user_id)
203 LEFT JOIN gapps_accounts AS g ON (u.user_id = g.l_userid AND g.g_status = 'active')
204 WHERE u.user_id = {?} AND u.perms IN('admin', 'user')", $uid);
205 $sess = $res->fetchOneAssoc();
206 $perms = $sess['perms'];
207 unset($sess['perms']);
209 // Retrieves account usage information (last login, last host).
210 $res = XDB
::query('SELECT UNIX_TIMESTAMP(s.start) AS lastlogin, s.host
211 FROM logger.sessions AS s
212 WHERE s.uid = {?} AND s.suid = 0
213 ORDER BY s.start DESC
215 if ($res->numRows()) {
216 $sess = array_merge($sess, $res->fetchOneAssoc());
219 // Loads the data into the real session.
220 $_SESSION = array_merge($_SESSION, $sess);
222 // Starts the session's logger, and sets up the permanent cookie.
223 if (S
::has('suid')) {
224 $suid = S
::v('suid');
225 $logger = S
::logger($uid);
226 $logger->log("suid_start", S
::v('hruid') . " by " . $suid['hruid']);
228 $logger = S
::logger($uid);
229 setcookie('ORGuid', $uid, (time() +
25920000), '/', '', 0);
231 if (S
::i('auth_by_cookie') == $uid || Post
::v('remember', 'false') == 'true') {
232 $cookie = hash_encrypt($sess['password']);
233 setcookie('ORGaccess', $cookie, (time() +
25920000), '/', '', 0);
234 if ($logger && S
::i('auth_by_cookie') != $uid) {
235 $logger->log("cookie_on");
238 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
240 $logger->log("cookie_off");
245 // Finalizes the session setup.
246 S
::set('perms', User
::makePerms($perms));
247 $this->securityChecks();
249 $this->updateNbNotifs();
252 // We should not have to use this private data anymore
253 S
::kill('auth_by_cookie');
257 private function securityChecks()
259 $mail_subject = array();
260 if (check_account()) {
261 $mail_subject[] = 'Connexion d\'un utilisateur surveillé';
263 if (check_ip('unsafe')) {
264 $mail_subject[] = 'Une IP surveillee a tente de se connecter';
265 if (check_ip('ban')) {
266 send_warning_mail(implode(' - ', $mail_subject));
268 Platal
::page()->kill('Une erreur est survenue lors de la procédure d\'authentification. '
269 . 'Merci de contacter au plus vite '
270 . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>');
274 if (count($mail_subject)) {
275 send_warning_mail(implode(' - ', $mail_subject));
279 public function tokenAuth($login, $token)
281 $res = XDB
::query('SELECT u.hruid
283 INNER JOIN auth_user_md5 AS u ON (a.id = u.user_id AND u.perms IN ("admin", "user"))
284 INNER JOIN auth_user_quick AS q ON (a.id = q.user_id AND q.core_rss_hash = {?})
285 WHERE a.alias = {?} AND a.type != "homonyme"', $token, $login);
286 if ($res->numRows() == 1) {
287 $data = $res->fetchOneAssoc();
288 return new User($data['hruid'], $data);
293 public function makePerms($perm)
295 $flags = new PlFlagSet();
296 if ($perm == 'disabled' ||
$perm == 'ext') {
297 S
::set('perms', $flags);
300 $flags->addFlag(PERMS_USER
);
301 if ($perm == 'admin') {
302 $flags->addFlag(PERMS_ADMIN
);
304 S
::set('perms', $flags);
307 public function setSkin()
309 if (S
::logged() && (!S
::has('skin') || S
::has('suid'))) {
311 $res = XDB
::query("SELECT skin_tpl
312 FROM auth_user_quick AS a
313 INNER JOIN skins AS s ON a.skin = s.id
314 WHERE user_id = {?} AND skin_tpl != ''", $uid);
315 S
::set('skin', $res->fetchOneCell());
319 public function loggedLevel()
324 public function sureLevel()
330 public function updateNbNotifs()
332 require_once 'notifs.inc.php';
333 $n = select_notifs(false
, S
::i('uid'), S
::v('watch_last'), false
);
334 S
::set('notifs', $n->numRows());
338 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: