| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2008 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 | class XorgSession extends PlSession |
| 23 | { |
| 24 | public function __construct() |
| 25 | { |
| 26 | parent::__construct(); |
| 27 | } |
| 28 | |
| 29 | public function startAvailableAuth() |
| 30 | { |
| 31 | if (!S::logged()) { |
| 32 | $cookie = $this->tryCookie(); |
| 33 | if ($cookie == 0) { |
| 34 | return $this->start(AUTH_COOKIE); |
| 35 | } else if ($cookie == 1 || $cookie == -2) { |
| 36 | return false; |
| 37 | } |
| 38 | } |
| 39 | if ((check_ip('dangerous') && S::has('uid')) || check_account()) { |
| 40 | S::logger()->log("view_page", $_SERVER['REQUEST_URI']); |
| 41 | } |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | /** Check the cookie and set the associated user_id in the auth_by_cookie session variable. |
| 46 | */ |
| 47 | private function tryCookie() |
| 48 | { |
| 49 | S::kill('auth_by_cookie'); |
| 50 | if (Cookie::v('access') == '' || !Cookie::has('uid')) { |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | $res = XDB::query('SELECT uid, password |
| 55 | FROM accounts |
| 56 | WHERE uid = {?} AND state = \'active\'', |
| 57 | Cookie::i('ORGuid')); |
| 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); |
| 63 | return 0; |
| 64 | } else { |
| 65 | return 1; |
| 66 | } |
| 67 | } |
| 68 | return -2; |
| 69 | } |
| 70 | |
| 71 | private function checkPassword($uname, $login, $response, $login_type) |
| 72 | { |
| 73 | $res = XDB::query('SELECT a.uid, a.password |
| 74 | FROM accounts AS a |
| 75 | INNER JOIN aliases AS l ON (l.id = a.uid AND l.type != \'homonyme\') |
| 76 | WHERE l.' . $login_type . ' = {?} AND a.state = \'active\'', |
| 77 | $login); |
| 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) { |
| 82 | S::logger($uid)->log('auth_fail', 'bad password'); |
| 83 | return null; |
| 84 | } |
| 85 | return $uid; |
| 86 | } |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /** Check auth. |
| 92 | */ |
| 93 | protected function doAuth($level) |
| 94 | { |
| 95 | global $globals; |
| 96 | |
| 97 | /* Cookie authentication |
| 98 | */ |
| 99 | if ($level == AUTH_COOKIE && !S::has('auth_by_cookie')) { |
| 100 | $this->tryCookie(); |
| 101 | } |
| 102 | if ($level == AUTH_COOKIE && S::has('auth_by_cookie')) { |
| 103 | if (!S::logged()) { |
| 104 | S::set('auth', AUTH_COOKIE); |
| 105 | } |
| 106 | return S::i('auth_by_cookie'); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /* We want to do auth... we must have infos from a form. |
| 111 | */ |
| 112 | if (!Post::has('username') || !Post::has('response') || !S::has('challenge')) { |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | /** We come from an authentication form. |
| 117 | */ |
| 118 | if (S::has('suid')) { |
| 119 | $suid = S::v('suid'); |
| 120 | $login = $uname = $suid['uid']; |
| 121 | $redirect = false; |
| 122 | } else { |
| 123 | $uname = Env::v('username'); |
| 124 | if (Env::v('domain') == "alias") { |
| 125 | $res = XDB::query('SELECT redirect |
| 126 | FROM virtual |
| 127 | INNER JOIN virtual_redirect USING(vid) |
| 128 | WHERE alias LIKE {?}', |
| 129 | $uname . '@' . $globals->mail->alias_dom); |
| 130 | $redirect = $res->fetchOneCell(); |
| 131 | if ($redirect) { |
| 132 | $login = substr($redirect, 0, strpos($redirect, '@')); |
| 133 | } else { |
| 134 | $login = ''; |
| 135 | } |
| 136 | } else { |
| 137 | $login = $uname; |
| 138 | $redirect = false; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | $uid = $this->checkPassword($uname, $login, Post::v('response'), (!$redirect && is_numeric($uname)) ? 'id' : 'alias'); |
| 143 | if (!is_null($uid) && S::has('suid')) { |
| 144 | $suid = S::v('suid'); |
| 145 | if ($suid['uid'] == $uid) { |
| 146 | $uid = S::i('uid'); |
| 147 | } else { |
| 148 | $uid = null; |
| 149 | } |
| 150 | } |
| 151 | if (!is_null($uid)) { |
| 152 | S::set('auth', AUTH_MDP); |
| 153 | if (!S::has('suid')) { |
| 154 | if (Post::has('domain')) { |
| 155 | if (($domain = Post::v('domain', 'login')) == 'alias') { |
| 156 | Cookie::set('domain', 'alias', 300); |
| 157 | } else { |
| 158 | Cookie::kill('domain'); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | S::kill('challenge'); |
| 163 | S::logger($uid)->log('auth_ok'); |
| 164 | } |
| 165 | return $uid; |
| 166 | } |
| 167 | |
| 168 | protected function startSessionAs($uid, $level) |
| 169 | { |
| 170 | if ((!is_null(S::v('user')) && S::i('user') != $uid) || (S::has('uid') && S::i('uid') != $uid)) { |
| 171 | return false; |
| 172 | } else if (S::has('uid')) { |
| 173 | return true; |
| 174 | } |
| 175 | if ($level == AUTH_SUID) { |
| 176 | S::set('auth', AUTH_MDP); |
| 177 | unset($_SESSION['log']); |
| 178 | } |
| 179 | |
| 180 | // Retrieves main user properties. |
| 181 | /** TODO: Move needed informations to account tables */ |
| 182 | /** TODO: Currently suppressed data are matricule, promo */ |
| 183 | /** TODO: Data to move are: banana_last, watch_last, last_version */ |
| 184 | /** TODO: Switch to new permission system */ |
| 185 | $res = XDB::query("SELECT a.uid, a.hruid, a.display_name, a.full_name, a.password, |
| 186 | a.sex = 'female' AS femme, a.mail_format as mail_fmt, |
| 187 | a.token, FIND_IN_SET('watch', a.flags) AS watch_account, |
| 188 | UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, |
| 189 | q.last_version, g.g_account_name IS NOT NULL AS googleapps, |
| 190 | UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, |
| 191 | IF(a.is_admin, 'admin', 'user') AS perms |
| 192 | FROM accounts AS a |
| 193 | INNER JOIN auth_user_quick AS q ON(a.uid = q.user_id) |
| 194 | LEFT JOIN gapps_accounts AS g ON(a.uid = g.l_userid AND g.g_status = 'active') |
| 195 | LEFT JOIN logger.last_sessions AS ls ON (ls.uid = a.uid) |
| 196 | LEFT JOIN logger.sessions AS s ON(s.id = ls.id) |
| 197 | WHERE a.uid = {?} AND a.state = 'active'", $uid); |
| 198 | $sess = $res->fetchOneAssoc(); |
| 199 | $perms = $sess['perms']; |
| 200 | unset($sess['perms']); |
| 201 | |
| 202 | // Loads the data into the real session. |
| 203 | $_SESSION = array_merge($_SESSION, $sess); |
| 204 | |
| 205 | // Starts the session's logger, and sets up the permanent cookie. |
| 206 | if (S::has('suid')) { |
| 207 | $suid = S::v('suid'); |
| 208 | $logger = S::logger($uid); |
| 209 | $logger->log("suid_start", S::v('hruid') . " by " . $suid['hruid']); |
| 210 | } else { |
| 211 | $logger = S::logger($uid); |
| 212 | $logger->saveLastSession(); |
| 213 | Cookie::set('uid', $uid, 300); |
| 214 | |
| 215 | if (S::i('auth_by_cookie') == $uid || Post::v('remember', 'false') == 'true') { |
| 216 | Cookie::set('access', sha1($sess['password']), 300); |
| 217 | if (S::i('auth_by_cookie') != $uid) { |
| 218 | $logger->log("cookie_on"); |
| 219 | } |
| 220 | } else { |
| 221 | Cookie::kill('access'); |
| 222 | $logger->log("cookie_off"); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // Finalizes the session setup. |
| 227 | S::set('perms', User::makePerms($perms)); |
| 228 | $this->securityChecks(); |
| 229 | $this->setSkin(); |
| 230 | $this->updateNbNotifs(); |
| 231 | check_redirect(); |
| 232 | |
| 233 | // We should not have to use this private data anymore |
| 234 | S::kill('auth_by_cookie'); |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | private function securityChecks() |
| 239 | { |
| 240 | $mail_subject = array(); |
| 241 | if (check_account()) { |
| 242 | $mail_subject[] = 'Connexion d\'un utilisateur surveillé'; |
| 243 | } |
| 244 | if (check_ip('unsafe')) { |
| 245 | $mail_subject[] = 'Une IP surveillee a tente de se connecter'; |
| 246 | if (check_ip('ban')) { |
| 247 | send_warning_mail(implode(' - ', $mail_subject)); |
| 248 | $this->destroy(); |
| 249 | Platal::page()->kill('Une erreur est survenue lors de la procédure d\'authentification. ' |
| 250 | . 'Merci de contacter au plus vite ' |
| 251 | . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>'); |
| 252 | return false; |
| 253 | } |
| 254 | } |
| 255 | if (count($mail_subject)) { |
| 256 | send_warning_mail(implode(' - ', $mail_subject)); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | public function tokenAuth($login, $token) |
| 261 | { |
| 262 | $res = XDB::query('SELECT a.hruid |
| 263 | FROM aliases AS l |
| 264 | INNER JOIN accounts AS a ON (l.id = a.uid AND a.state = \'active\') |
| 265 | WHERE a.token = {?} AND l.alias = {?} AND l.type != \'homonyme\'', |
| 266 | $token, $login); |
| 267 | if ($res->numRows() == 1) { |
| 268 | $data = $res->fetchOneAssoc(); |
| 269 | return new User($data['hruid'], $data); |
| 270 | } |
| 271 | return null; |
| 272 | } |
| 273 | |
| 274 | public function makePerms($perm) |
| 275 | { |
| 276 | $flags = new PlFlagSet(); |
| 277 | if ($perm == 'disabled' || $perm == 'ext') { |
| 278 | S::set('perms', $flags); |
| 279 | return; |
| 280 | } |
| 281 | $flags->addFlag(PERMS_USER); |
| 282 | if ($perm == 'admin') { |
| 283 | $flags->addFlag(PERMS_ADMIN); |
| 284 | } |
| 285 | S::set('perms', $flags); |
| 286 | } |
| 287 | |
| 288 | public function setSkin() |
| 289 | { |
| 290 | if (S::logged() && (!S::has('skin') || S::has('suid'))) { |
| 291 | $uid = S::v('uid'); |
| 292 | $res = XDB::query('SELECT skin_tpl |
| 293 | FROM accounts AS a |
| 294 | INNER JOIN skins AS s on (a.skin = s.id) |
| 295 | WHERE a.uid = {?} AND skin_tpl != \'\'', S::i('uid')); |
| 296 | S::set('skin', $res->fetchOneCell()); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | public function loggedLevel() |
| 301 | { |
| 302 | return AUTH_COOKIE; |
| 303 | } |
| 304 | |
| 305 | public function sureLevel() |
| 306 | { |
| 307 | return AUTH_MDP; |
| 308 | } |
| 309 | |
| 310 | |
| 311 | public function updateNbNotifs() |
| 312 | { |
| 313 | require_once 'notifs.inc.php'; |
| 314 | $n = select_notifs(false, S::i('uid'), S::v('watch_last'), false); |
| 315 | S::set('notifs', $n->numRows()); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 320 | ?> |