Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
179afa7f | 3 | * Copyright (C) 2003-2008 Polytechnique.org * |
0337d704 | 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 | ||
c0799142 | 22 | class XorgSession extends PlSession |
0337d704 | 23 | { |
c0799142 FB |
24 | public function __construct() |
25 | { | |
26 | parent::__construct(); | |
27 | S::bootstrap('perms_backup', new PlFlagSet()); | |
abde67b1 FB |
28 | } |
29 | ||
c0799142 | 30 | public function startAvailableAuth() |
5480a216 | 31 | { |
c0799142 FB |
32 | if (!(S::v('perms') instanceof PlFlagSet)) { |
33 | S::set('perms', S::v('perms_backup')); | |
34 | } | |
35 | if (!S::logged()) { | |
36 | $cookie = $this->tryCookie(); | |
37 | if ($cookie == 0) { | |
38 | return $this->start(AUTH_COOKIE); | |
39 | } else if ($cookie == 1 || $cooke == -2) { | |
40 | return false; | |
41 | } | |
5480a216 | 42 | } |
0be07aa6 | 43 | if ((check_ip('dangerous') && S::has('uid')) || check_account()) { |
5480a216 | 44 | $_SESSION['log']->log("view_page", $_SERVER['REQUEST_URI']); |
0337d704 | 45 | } |
c0799142 | 46 | return true; |
0337d704 | 47 | } |
48 | ||
c0799142 FB |
49 | /** Check the cookie and set the associated user_id in the auth_by_cookie session variable. |
50 | */ | |
51 | private function tryCookie() | |
52 | { | |
732e5855 | 53 | Platal::page()->trigError("Trying cookie"); |
c0799142 FB |
54 | S::kill('auth_by_cookie'); |
55 | if (Cookie::v('ORGaccess') == '' || !Cookie::has('ORGuid')) { | |
56 | return -1; | |
57 | } | |
cab08090 | 58 | |
c0799142 FB |
59 | $res = XDB::query('SELECT user_id, password |
60 | FROM auth_user_md5 | |
61 | WHERE user_id = {?} AND perms IN(\'admin\', \'user\')', | |
62 | Cookie::i('ORGuid')); | |
63 | if ($res->numRows() != 0) { | |
64 | list($uid, $password) = $res->fetchOneRow(); | |
65 | require_once 'secure_hash.inc.php'; | |
66 | $expected_value = hash_encrypt($password); | |
67 | if ($expected_value == Cookie::v('ORGaccess')) { | |
68 | S::set('auth_by_cookie', $uid); | |
69 | return 0; | |
70 | } else { | |
71 | return 1; | |
72 | } | |
73 | } | |
74 | return -2; | |
75 | } | |
76 | ||
77 | private function checkPassword($uname, $login, $response, $login_type) | |
5480a216 | 78 | { |
c0799142 FB |
79 | $res = XDB::query('SELECT u.user_id, u.password |
80 | FROM auth_user_md5 AS u | |
81 | INNER JOIN aliases AS a ON (a.id = u.user_id AND type != \'homonyme\') | |
82 | WHERE a.' . $login_type . ' = {?} AND u.perms IN(\'admin\', \'user\')', | |
83 | $login); | |
84 | if (list($uid, $password) = $res->fetchOneRow()) { | |
85 | require_once 'secure_hash.inc.php'; | |
86 | $expected_response = hash_encrypt("$uname:$password:" . S::v('challenge')); | |
87 | if ($response != $expected_response) { | |
88 | $new_password = hash_xor(Env::v('xorpass'), $password); | |
89 | $expected_response = hash_encrypt("$uname:$new_password:" . S::v('challenge')); | |
90 | if ($response == $expected_response) { | |
91 | XDB::execute('UPDATE auth_user_md5 | |
92 | SET password = {?} | |
93 | WHERE user_id = {?}', | |
94 | $new_password, $uid); | |
95 | } | |
96 | } | |
97 | if ($response != $expected_response) { | |
732e5855 | 98 | S::logger($uid)->log('auth_fail', 'bad password'); |
c0799142 FB |
99 | return null; |
100 | } | |
101 | return $uid; | |
102 | } | |
103 | return null; | |
0337d704 | 104 | } |
cab08090 | 105 | |
0337d704 | 106 | |
c0799142 FB |
107 | /** Check auth. |
108 | */ | |
109 | protected function doAuth($level) | |
0337d704 | 110 | { |
e74411f7 | 111 | global $globals; |
c0799142 FB |
112 | |
113 | /* Cookie authentication | |
114 | */ | |
115 | if ($level == AUTH_COOKIE && !S::has('auth_by_cookie')) { | |
116 | $this->tryCookie(); | |
117 | } | |
118 | if ($level == AUTH_COOKIE && S::has('auth_by_cookie')) { | |
119 | if (!S::logged()) { | |
120 | S::set('auth', AUTH_COOKIE); | |
121 | } | |
122 | return S::i('auth_by_cookie'); | |
e74411f7 | 123 | } |
0337d704 | 124 | |
c0799142 FB |
125 | |
126 | /* We want to do auth... we must have infos from a form. | |
127 | */ | |
128 | if (!Env::has('username') || !Env::has('response') || !S::has('challenge')) { | |
129 | return null; | |
63528107 | 130 | } |
131 | ||
c0799142 FB |
132 | /** We come from an authentication form. |
133 | */ | |
e74411f7 | 134 | if (S::has('suid')) { |
c0799142 | 135 | $suid = S::v('suid'); |
e74411f7 | 136 | $login = $uname = $suid['forlife']; |
137 | $redirect = false; | |
138 | } else { | |
139 | $uname = Env::v('username'); | |
140 | ||
141 | if (Env::v('domain') == "alias") { | |
c0799142 FB |
142 | $res = XDB::query('SELECT redirect |
143 | FROM virtual | |
144 | INNER JOIN virtual_redirect USING(vid) | |
145 | WHERE alias LIKE {?}', | |
146 | $uname . '@' . $globals->mail->alias_dom); | |
e74411f7 | 147 | $redirect = $res->fetchOneCell(); |
148 | if ($redirect) { | |
149 | $login = substr($redirect, 0, strpos($redirect, '@')); | |
150 | } else { | |
c0799142 | 151 | $login = ''; |
e74411f7 | 152 | } |
0337d704 | 153 | } else { |
e74411f7 | 154 | $login = $uname; |
155 | $redirect = false; | |
0337d704 | 156 | } |
63528107 | 157 | } |
cab08090 | 158 | |
c0799142 FB |
159 | $uid = $this->checkPassword($uname, $login, Post::v('response'), (!$redirect && preg_match('/^\d*$/', $uname)) ? 'id' : 'alias'); |
160 | if (!is_null($uid)) { | |
161 | S::set('auth', AUTH_MDP); | |
162 | if (Post::has('domain')) { | |
163 | if (($domain = Post::v('domain', 'login')) == 'alias') { | |
164 | setcookie('ORGdomain', "alias", (time() + 25920000), '/', '', 0); | |
165 | } else { | |
166 | setcookie('ORGdomain', '', (time() - 3600), '/', '', 0); | |
e74411f7 | 167 | } |
c0799142 FB |
168 | // pour que la modification soit effective dans le reste de la page |
169 | $_COOKIE['ORGdomain'] = $domain; | |
e74411f7 | 170 | } |
c0799142 | 171 | S::kill('challenge'); |
732e5855 | 172 | S::logger($uid)->log('auth_ok'); |
c0799142 FB |
173 | } |
174 | return $uid; | |
175 | } | |
cab08090 | 176 | |
c0799142 FB |
177 | protected function startSessionAs($uid, $level) |
178 | { | |
179 | if ((!is_null(S::v('user')) && S::i('user') != $uid) || (S::has('uid') && S::i('uid') != $uid)) { | |
180 | return false; | |
181 | } else if (S::has('uid')) { | |
182 | return true; | |
183 | } | |
184 | if ($level == -1) { | |
185 | S::set('auth', AUTH_COOKIE); | |
186 | } | |
187 | unset($_SESSION['log']); | |
188 | $res = XDB::query('SELECT u.user_id AS uid, prenom, prenom_ini, nom, nom_ini, nom_usage, perms, promo, promo_sortie, | |
189 | matricule, password, FIND_IN_SET(\'femme\', u.flags) AS femme, | |
190 | a.alias AS forlife, a2.alias AS bestalias, | |
191 | q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash, | |
192 | FIND_IN_SET(\'watch\', u.flags) AS watch_account, q.last_version | |
193 | FROM auth_user_md5 AS u | |
194 | INNER JOIN auth_user_quick AS q USING(user_id) | |
195 | INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\') | |
196 | INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET(\'bestalias\', a2.flags)) | |
197 | WHERE u.user_id = {?} AND u.perms IN(\'admin\', \'user\')', $uid); | |
198 | $sess = $res->fetchOneAssoc(); | |
199 | $perms = $sess['perms']; | |
200 | unset($sess['perms']); | |
201 | $res = XDB::query('SELECT UNIX_TIMESTAMP(s.start) AS lastlogin, s.host | |
202 | FROM logger.sessions AS s | |
203 | WHERE s.uid = {?} AND s.suid = 0 | |
204 | ORDER BY s.start DESC | |
205 | LIMIT 1', $uid); | |
206 | if ($res->numRows()) { | |
207 | $sess = array_merge($sess, $res->fetchOneAssoc()); | |
208 | } | |
209 | $suid = S::v('suid'); | |
210 | ||
211 | if ($suid) { | |
212 | $logger = S::logger(); | |
213 | $logger->log("suid_start", S::v('forlife')." by {$suid['uid']}"); | |
214 | $sess['suid'] = $suid; | |
215 | } else { | |
216 | $logger = S::logger(); | |
217 | //$logger->log("connexion", Env::v('n')); | |
218 | setcookie('ORGuid', $uid, (time() + 25920000), '/', '', 0); | |
219 | if (Post::v('remember', 'false') == 'true') { | |
220 | $cookie = hash_encrypt($sess['password']); | |
221 | setcookie('ORGaccess', $cookie, (time() + 25920000), '/', '', 0); | |
63528107 | 222 | if ($logger) { |
c0799142 | 223 | $logger->log("cookie_on"); |
63528107 | 224 | } |
c0799142 FB |
225 | } else { |
226 | setcookie('ORGaccess', '', time() - 3600, '/', '', 0); | |
227 | if ($logger) { | |
228 | $logger->log("cookie_off"); | |
0337d704 | 229 | } |
0337d704 | 230 | } |
63528107 | 231 | } |
b0b937fd | 232 | |
c0799142 FB |
233 | $_SESSION = array_merge($_SESSION, $sess); |
234 | $this->makePerms($perms); | |
235 | $this->securityChecks(); | |
236 | $this->setSkin(); | |
237 | update_NbNotifs(); | |
238 | check_redirect(); | |
239 | return true; | |
0337d704 | 240 | } |
241 | ||
c0799142 | 242 | private function securityChecks() |
0337d704 | 243 | { |
c0799142 FB |
244 | $mail_subject = array(); |
245 | if (check_account()) { | |
246 | $mail_subject[] = 'Connexion d\'un utilisateur surveillé'; | |
0337d704 | 247 | } |
c0799142 FB |
248 | if (check_ip('unsafe')) { |
249 | $mail_subject[] = 'Une IP surveillee a tente de se connecter'; | |
250 | if (check_ip('ban')) { | |
251 | send_warning_mail(implode(' - ', $mail_subject)); | |
252 | $this->destroy(); | |
253 | Platal::page()->kill('Une erreur est survenue lors de la procédure d\'authentification. ' | |
254 | . 'Merci de contacter au plus vite ' | |
255 | . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>'); | |
256 | return false; | |
257 | } | |
0337d704 | 258 | } |
c0799142 FB |
259 | if (count($mail_subject)) { |
260 | send_warning_mail(implode(' - ', $mail_subject)); | |
0337d704 | 261 | } |
262 | } | |
263 | ||
732e5855 | 264 | public function makePerms($perm) |
bf517daf | 265 | { |
113f6de8 | 266 | $flags = new PlFlagSet(); |
bf517daf | 267 | if ($perm == 'disabled' || $perm == 'ext') { |
ab694eb5 FB |
268 | S::set('perms', $flags); |
269 | S::set('perms_backup', $flags); | |
270 | return; | |
bf517daf | 271 | } |
272 | $flags->addFlag(PERMS_USER); | |
273 | if ($perm == 'admin') { | |
274 | $flags->addFlag(PERMS_ADMIN); | |
275 | } | |
c0799142 FB |
276 | S::set('perms', $flags); |
277 | S::set('perms_backup', $flags); | |
bf517daf | 278 | } |
279 | ||
c0799142 FB |
280 | public function setSkin() |
281 | { | |
282 | global $globals; | |
283 | if (S::logged() && (!S::has('skin') || S::has('suid'))) { | |
284 | $uid = S::v('uid'); | |
285 | $res = XDB::query("SELECT skin_tpl | |
286 | FROM auth_user_quick AS a | |
287 | INNER JOIN skins AS s ON a.skin = s.id | |
288 | WHERE user_id = {?} AND skin_tpl != ''", $uid); | |
289 | S::set('skin', $res->fetchOneCell()); | |
5480a216 | 290 | } |
291 | } | |
0337d704 | 292 | |
c0799142 FB |
293 | public function sureLevel() |
294 | { | |
295 | return AUTH_MDP; | |
0337d704 | 296 | } |
0337d704 | 297 | } |
298 | ||
a7de4ef7 | 299 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 300 | ?> |