| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2006 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 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. * |
| 10 | * * |
| 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. * |
| 15 | * * |
| 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 * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | require_once dirname(__FILE__).'/../../classes/Session.php'; |
| 23 | |
| 24 | // {{{ class XorgSession |
| 25 | |
| 26 | class XorgSession |
| 27 | { |
| 28 | // {{{ function init |
| 29 | |
| 30 | function init() { |
| 31 | S::init(); |
| 32 | if (!S::has('uid')) { |
| 33 | try_cookie(); |
| 34 | } |
| 35 | $_SESSION['session'] = new XorgSession; |
| 36 | } |
| 37 | |
| 38 | // }}} |
| 39 | // {{{ function destroy() |
| 40 | |
| 41 | function destroy() { |
| 42 | S::destroy(); |
| 43 | XorgSession::init(); |
| 44 | } |
| 45 | |
| 46 | // }}} |
| 47 | // {{{ function doAuth() |
| 48 | |
| 49 | function doAuth($new_name = false) |
| 50 | { |
| 51 | global $globals; |
| 52 | if (S::identified()) { // ok, c'est bon, on n'a rien à faire |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | if (!Env::has('username') || !Env::has('response') |
| 57 | || !S::has('challenge')) |
| 58 | { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | // si on vient de recevoir une identification par passwordpromptscreen.tpl |
| 63 | // ou passwordpromptscreenlogged.tpl |
| 64 | $uname = Env::v('username'); |
| 65 | |
| 66 | if (Env::v('domain') == "alias") { |
| 67 | |
| 68 | $res = XDB::query( |
| 69 | "SELECT redirect |
| 70 | FROM virtual |
| 71 | INNER JOIN virtual_redirect USING(vid) |
| 72 | WHERE alias LIKE {?}", $uname."@".$globals->mail->alias_dom); |
| 73 | $redirect = $res->fetchOneCell(); |
| 74 | if ($redirect) { |
| 75 | $login = substr($redirect, 0, strpos($redirect, '@')); |
| 76 | } else { |
| 77 | $login = ""; |
| 78 | } |
| 79 | } else { |
| 80 | $login = $uname; |
| 81 | } |
| 82 | |
| 83 | $field = (!$redirect && preg_match('/^\d*$/', $uname)) ? 'id' : 'alias'; |
| 84 | $res = XDB::query( |
| 85 | "SELECT u.user_id, u.password |
| 86 | FROM auth_user_md5 AS u |
| 87 | INNER JOIN aliases AS a ON ( a.id=u.user_id AND type!='homonyme' ) |
| 88 | WHERE a.$field = {?} AND u.perms IN('admin','user')", $login); |
| 89 | |
| 90 | $logger =& S::v('log'); |
| 91 | if (list($uid, $password) = $res->fetchOneRow()) { |
| 92 | require_once('secure_hash.inc.php'); |
| 93 | $expected_response=hash_encrypt("$uname:$password:".S::v('challenge')); |
| 94 | // le password de la base est peut-être encore encodé en md5 |
| 95 | if (Env::v('response') != $expected_response) { |
| 96 | $new_password = hash_xor(Env::v('xorpass'), $password); |
| 97 | $expected_response = hash_encrypt("$uname:$new_password:".S::v('challenge')); |
| 98 | if (Env::v('response') == $expected_response) { |
| 99 | XDB::execute("UPDATE auth_user_md5 SET password = {?} WHERE user_id = {?}", $new_password, $uid); |
| 100 | } |
| 101 | } |
| 102 | if (Env::v('response') == $expected_response) { |
| 103 | if (Env::has('domain')) { |
| 104 | if (($domain = Env::v('domain', 'login')) == 'alias') { |
| 105 | setcookie('ORGdomain', "alias", (time()+25920000), '/', '', 0); |
| 106 | } else { |
| 107 | setcookie('ORGdomain', '', (time()-3600), '/', '', 0); |
| 108 | } |
| 109 | // pour que la modification soit effective dans le reste de la page |
| 110 | $_COOKIE['ORGdomain'] = $domain; |
| 111 | } |
| 112 | |
| 113 | S::kill('challenge'); |
| 114 | if ($logger) { |
| 115 | $logger->log('auth_ok'); |
| 116 | } |
| 117 | start_connexion($uid, true); |
| 118 | if (Env::v('remember', 'false') == 'true') { |
| 119 | $cookie = hash_encrypt(S::v('password')); |
| 120 | setcookie('ORGaccess',$cookie,(time()+25920000),'/','',0); |
| 121 | if ($logger) { |
| 122 | $logger->log("cookie_on"); |
| 123 | } |
| 124 | } else { |
| 125 | setcookie('ORGaccess', '', time() - 3600, '/', '', 0); |
| 126 | |
| 127 | if ($logger) { |
| 128 | $logger->log("cookie_off"); |
| 129 | } |
| 130 | } |
| 131 | return true; |
| 132 | } elseif ($logger) { |
| 133 | $logger->log('auth_fail','bad password'); |
| 134 | } |
| 135 | } elseif ($logger) { |
| 136 | $logger->log('auth_fail','bad login'); |
| 137 | } |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | // }}} |
| 143 | // {{{ function doAuthCookie() |
| 144 | |
| 145 | /** Try to do a cookie-based authentication. |
| 146 | * |
| 147 | * @param page the calling page (by reference) |
| 148 | */ |
| 149 | function doAuthCookie() |
| 150 | { |
| 151 | if (S::logged()) { |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | if (Env::has('username') and Env::has('response')) { |
| 156 | return XorgSession::doAuth(); |
| 157 | } |
| 158 | |
| 159 | if ($r = try_cookie()) { |
| 160 | return XorgSession::doAuth(($r > 0)); |
| 161 | } |
| 162 | |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | // }}} |
| 167 | } |
| 168 | |
| 169 | // }}} |
| 170 | // {{{ function try_cookie() |
| 171 | |
| 172 | /** réalise la récupération de $_SESSION pour qqn avec cookie |
| 173 | * @return int 0 if all OK, -1 if no cookie, 1 if cookie with bad hash, |
| 174 | * -2 should not happen |
| 175 | */ |
| 176 | function try_cookie() |
| 177 | { |
| 178 | if (Cookie::v('ORGaccess') == '' or !Cookie::has('ORGuid')) { |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | $res = @XDB::query( |
| 183 | "SELECT user_id,password FROM auth_user_md5 WHERE user_id = {?} AND perms IN('admin','user')", |
| 184 | Cookie::i('ORGuid') |
| 185 | ); |
| 186 | if ($res->numRows() != 0) { |
| 187 | list($uid, $password) = $res->fetchOneRow(); |
| 188 | require_once('secure_hash.inc.php'); |
| 189 | $expected_value = hash_encrypt($password); |
| 190 | if ($expected_value == Cookie::v('ORGaccess')) { |
| 191 | start_connexion($uid, false); |
| 192 | return 0; |
| 193 | } else { |
| 194 | return 1; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return -2; |
| 199 | } |
| 200 | |
| 201 | // }}} |
| 202 | // {{{ function start_connexion() |
| 203 | |
| 204 | /** place les variables de session dépendants de auth_user_md5 |
| 205 | * et met à jour les dates de dernière connexion si nécessaire |
| 206 | * @return void |
| 207 | * @see controlpermanent.inc.php controlauthentication.inc.php |
| 208 | */ |
| 209 | function start_connexion ($uid, $identified) |
| 210 | { |
| 211 | $res = XDB::query(" |
| 212 | SELECT u.user_id AS uid, prenom, nom, perms, promo, matricule, password, FIND_IN_SET('femme', u.flags) AS femme, |
| 213 | UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, a.alias AS forlife, a2.alias AS bestalias, |
| 214 | q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash |
| 215 | FROM auth_user_md5 AS u |
| 216 | INNER JOIN auth_user_quick AS q USING(user_id) |
| 217 | INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie') |
| 218 | INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET('bestalias',a2.flags)) |
| 219 | LEFT JOIN logger.sessions AS s ON (s.uid=u.user_id AND s.suid=0) |
| 220 | WHERE u.user_id = {?} AND u.perms IN('admin','user') |
| 221 | ORDER BY s.start DESC |
| 222 | LIMIT 1", $uid); |
| 223 | $sess = $res->fetchOneAssoc(); |
| 224 | $suid = S::v('suid'); |
| 225 | |
| 226 | if ($suid) { |
| 227 | $logger = new CoreLogger($uid, $suid); |
| 228 | $logger->log("suid_start", S::v('forlife')." by {$suid['uid']}"); |
| 229 | $sess['suid'] = $suid; |
| 230 | } else { |
| 231 | $logger = S::v('log', new CoreLogger($uid)); |
| 232 | $logger->log("connexion", $_SERVER['PHP_SELF']); |
| 233 | setcookie('ORGuid', $uid, (time()+25920000), '/', '', 0); |
| 234 | } |
| 235 | |
| 236 | $_SESSION = $sess; |
| 237 | $_SESSION['log'] = $logger; |
| 238 | $_SESSION['auth'] = ($identified ? AUTH_MDP : AUTH_COOKIE); |
| 239 | set_skin(); |
| 240 | } |
| 241 | |
| 242 | // }}} |
| 243 | |
| 244 | function set_skin() |
| 245 | { |
| 246 | global $globals; |
| 247 | if (S::logged() && !S::has('skin')) { |
| 248 | $uid = S::v('uid'); |
| 249 | $res = XDB::query("SELECT skin_tpl |
| 250 | FROM auth_user_quick AS a |
| 251 | INNER JOIN skins AS s ON a.skin = s.id |
| 252 | WHERE user_id = {?} AND skin_tpl != ''", $uid); |
| 253 | if ($_SESSION['skin'] = $res->fetchOneCell()) { |
| 254 | return; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: |
| 260 | ?> |