75b69a2f7039956240331d0d6673db08646d53aa
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 require_once 'xorg.misc.inc.php';
26 // {{{ public static function init
28 public static function init()
34 if ((check_ip('dangerous') && S
::has('uid')) ||
check_account()) {
35 $_SESSION['log']->log("view_page", $_SERVER['REQUEST_URI']);
40 // {{{ public static function destroy()
42 public static function destroy()
49 // {{{ public static function doAuth()
51 public static function doAuth($new_name = false
)
54 if (S
::identified()) { // ok, c'est bon, on n'a rien à faire
58 if (!Env
::has('username') ||
!Env
::has('response')
59 ||
!S
::has('challenge'))
64 // si on vient de recevoir une identification par passwordpromptscreen.tpl
65 // ou passwordpromptscreenlogged.tpl
68 $login = $uname = $suid['forlife'];
71 $uname = Env
::v('username');
73 if (Env
::v('domain') == "alias") {
77 INNER JOIN virtual_redirect USING(vid)
78 WHERE alias LIKE {?}", $uname."@".$globals->mail
->alias_dom
);
79 $redirect = $res->fetchOneCell();
81 $login = substr($redirect, 0, strpos($redirect, '@'));
91 $field = (!$redirect && preg_match('/^\d*$/', $uname)) ?
'id' : 'alias';
93 "SELECT u.user_id, u.password
94 FROM auth_user_md5 AS u
95 INNER JOIN aliases AS a ON ( a.id=u.user_id AND type!='homonyme' )
96 WHERE a.$field = {?} AND u.perms IN('admin','user')", $login);
98 $logger = S
::v('log');
99 if (list($uid, $password) = $res->fetchOneRow()) {
100 require_once('secure_hash.inc.php');
101 $expected_response = hash_encrypt("$uname:$password:".S
::v('challenge'));
102 // le password de la base est peut-être encore encodé en md5
103 if (Env
::v('response') != $expected_response) {
104 $new_password = hash_xor(Env
::v('xorpass'), $password);
105 $expected_response = hash_encrypt("$uname:$new_password:".S
::v('challenge'));
106 if (Env
::v('response') == $expected_response) {
107 XDB
::execute("UPDATE auth_user_md5 SET password = {?} WHERE user_id = {?}",
108 $new_password, $uid);
111 if (Env
::v('response') == $expected_response) {
112 if (Env
::has('domain')) {
113 if (($domain = Env
::v('domain', 'login')) == 'alias') {
114 setcookie('ORGdomain', "alias", (time()+
25920000), '/', '', 0);
116 setcookie('ORGdomain', '', (time()-3600), '/', '', 0);
118 // pour que la modification soit effective dans le reste de la page
119 $_COOKIE['ORGdomain'] = $domain;
122 S
::kill('challenge');
124 $logger->log('auth_ok');
126 if (!start_connexion($uid, true
)) {
129 if (Env
::v('remember', 'false') == 'true') {
130 $cookie = hash_encrypt(S
::v('password'));
131 setcookie('ORGaccess',$cookie,(time()+
25920000),'/','',0);
133 $logger->log("cookie_on");
136 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
139 $logger->log("cookie_off");
144 $logger->log('auth_fail','bad password');
147 $logger->log('auth_fail','bad login');
154 // {{{ public static function doAuthCookie()
156 /** Try to do a cookie-based authentication.
158 * @param page the calling page (by reference)
160 public static function doAuthCookie()
167 if (Env
::has('username') and Env
::has('response')) {
168 return XorgSession
::doAuth();
171 if ($r = try_cookie()) {
172 return XorgSession
::doAuth(($r > 0));
179 // {{{ public static function make_perms()
181 public static function &make_perms($perm)
183 $flags = new FlagSet();
184 if ($perm == 'disabled' ||
$perm == 'ext') {
187 $flags->addFlag(PERMS_USER
);
188 if ($perm == 'admin') {
189 $flags->addFlag(PERMS_ADMIN
);
197 // {{{ function try_cookie()
199 /** réalise la récupération de $_SESSION pour qqn avec cookie
200 * @return int 0 if all OK, -1 if no cookie, 1 if cookie with bad hash,
201 * -2 should not happen
203 function try_cookie()
205 if (Cookie
::v('ORGaccess') == '' or !Cookie
::has('ORGuid')) {
210 "SELECT user_id,password FROM auth_user_md5
211 WHERE user_id = {?} AND perms IN('admin','user')",
212 Cookie
::i('ORGuid'));
214 if ($res->numRows() != 0) {
215 list($uid, $password) = $res->fetchOneRow();
216 require_once('secure_hash.inc.php');
217 $expected_value = hash_encrypt($password);
218 if ($expected_value == Cookie
::v('ORGaccess')) {
219 if (!start_connexion($uid, false
)) {
232 // {{{ function start_connexion()
234 /** place les variables de session dépendants de auth_user_md5
235 * et met à jour les dates de dernière connexion si nécessaire
237 * @see controlpermanent.inc.php controlauthentication.inc.php
239 function start_connexion ($uid, $identified)
242 SELECT u.user_id AS uid, nom_usage, perms, promo, promo_sortie,
243 matricule, password, FIND_IN_SET('femme', u.flags) AS femme,
244 a.alias AS forlife, a2.alias AS bestalias,
245 q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash,
246 FIND_IN_SET('watch', u.flags) AS watch_account, q.last_version,
247 nd.display AS display_name, nd.yourself AS yourself_name,
248 nd.first_name AS prenom, nd.last_name AS nom
249 FROM auth_user_md5 AS u
250 INNER JOIN auth_user_quick AS q USING(user_id)
251 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
252 INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET('bestalias',a2.flags))
253 INNER JOIN profile_names_display AS nd ON (u.user_id = nd.user_id)
254 WHERE u.user_id = {?} AND u.perms IN('admin','user')", $uid);
255 $sess = $res->fetchOneAssoc();
256 $res = XDB
::query("SELECT UNIX_TIMESTAMP(s.start) AS lastlogin, s.host
257 FROM logger.sessions AS s
258 WHERE s.uid = {?} AND s.suid = 0
259 ORDER BY s.start DESC
261 if ($res->numRows()) {
262 $sess = array_merge($sess, $res->fetchOneAssoc());
264 $suid = S
::v('suid');
267 $logger = new CoreLogger($uid, $suid['uid']);
268 $logger->log("suid_start", S
::v('forlife')." by {$suid['uid']}");
269 $sess['suid'] = $suid;
271 $logger = S
::v('log', new CoreLogger($uid));
272 $logger->log("connexion", Env
::v('n'));
273 setcookie('ORGuid', $uid, (time()+
25920000), '/', '', 0);
276 $_SESSION = array_merge($_SESSION, $sess);
277 $_SESSION['log'] = $logger;
278 $_SESSION['auth'] = ($identified ? AUTH_MDP
: AUTH_COOKIE
);
279 $_SESSION['perms'] =& XorgSession
::make_perms($_SESSION['perms']);
280 $mail_subject = null
;
281 if (check_account()) {
282 $mail_subject = "Connexion d'un utilisateur surveillé";
284 if (check_ip('unsafe')) {
286 $mail_subject .= ' - ';
288 $mail_subject .= "Une IP surveillee a tente de se connecter";
289 if (check_ip('ban')) {
290 send_warning_mail($mail_subject);
292 $_SESSION['perms'] = new FlagSet();
296 require_once 'xorg.inc.php';
297 new_skinned_page('platal/index.tpl');
300 $page->trigError("Une erreur est survenue lors de la procédure d'authentification. "
301 ."Merci de contacter au plus vite "
302 ."<a href='mailto:support@polytechnique.org'>support@polytechnique.org</a>");
310 send_warning_mail($mail_subject);
323 if (S
::logged() && (!S
::has('skin') || S
::has('suid'))) {
325 $res = XDB
::query("SELECT skin_tpl
326 FROM auth_user_quick AS a
327 INNER JOIN skins AS s ON a.skin = s.id
328 WHERE user_id = {?} AND skin_tpl != ''", $uid);
329 if ($_SESSION['skin'] = $res->fetchOneCell()) {
335 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: