0337d704 |
1 | <?php |
2 | /*************************************************************************** |
50a40a33 |
3 | * Copyright (C) 2003-2006 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 | |
b76f0797 |
22 | require_once dirname(__FILE__).'/../../classes/Session.php'; |
0337d704 |
23 | |
24 | // {{{ class XorgSession |
25 | |
4869f665 |
26 | class XorgSession |
0337d704 |
27 | { |
cab08090 |
28 | // {{{ function init |
4869f665 |
29 | |
cab08090 |
30 | function init() { |
31 | S::init(); |
32 | if (!S::has('uid')) { |
0337d704 |
33 | try_cookie(); |
34 | } |
cab08090 |
35 | $_SESSION['session'] = new XorgSession; |
0337d704 |
36 | } |
37 | |
38 | // }}} |
0337d704 |
39 | // {{{ function destroy() |
cab08090 |
40 | |
0337d704 |
41 | function destroy() { |
cab08090 |
42 | S::destroy(); |
0337d704 |
43 | XorgSession::init(); |
44 | } |
cab08090 |
45 | |
0337d704 |
46 | // }}} |
47 | // {{{ function doAuth() |
48 | |
b0b937fd |
49 | function doAuth($new_name = false) |
0337d704 |
50 | { |
46bde4d1 |
51 | global $globals; |
cab08090 |
52 | if (S::identified()) { // ok, c'est bon, on n'a rien à faire |
46bde4d1 |
53 | return true; |
54 | } |
0337d704 |
55 | |
63528107 |
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 |
5e2307dc |
64 | $uname = Env::v('username'); |
63528107 |
65 | |
5e2307dc |
66 | if (Env::v('domain') == "alias") { |
63528107 |
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, '@')); |
0337d704 |
76 | } else { |
63528107 |
77 | $login = ""; |
0337d704 |
78 | } |
63528107 |
79 | } else { |
80 | $login = $uname; |
81 | } |
cab08090 |
82 | |
63528107 |
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 |
5e2307dc |
95 | if (Env::v('response') != $expected_response) { |
96 | $new_password = hash_xor(Env::v('xorpass'), $password); |
63528107 |
97 | $expected_response = hash_encrypt("$uname:$new_password:".S::v('challenge')); |
5e2307dc |
98 | if (Env::v('response') == $expected_response) { |
63528107 |
99 | XDB::execute("UPDATE auth_user_md5 SET password = {?} WHERE user_id = {?}", $new_password, $uid); |
100 | } |
101 | } |
5e2307dc |
102 | if (Env::v('response') == $expected_response) { |
63528107 |
103 | if (Env::has('domain')) { |
5e2307dc |
104 | if (($domain = Env::v('domain', 'login')) == 'alias') { |
63528107 |
105 | setcookie('ORGdomain', "alias", (time()+25920000), '/', '', 0); |
106 | } else { |
107 | setcookie('ORGdomain', '', (time()-3600), '/', '', 0); |
0337d704 |
108 | } |
63528107 |
109 | // pour que la modification soit effective dans le reste de la page |
110 | $_COOKIE['ORGdomain'] = $domain; |
111 | } |
cab08090 |
112 | |
63528107 |
113 | S::kill('challenge'); |
114 | if ($logger) { |
115 | $logger->log('auth_ok'); |
116 | } |
117 | start_connexion($uid, true); |
5e2307dc |
118 | if (Env::v('remember', 'false') == 'true') { |
63528107 |
119 | $cookie = hash_encrypt(S::v('password')); |
120 | setcookie('ORGaccess',$cookie,(time()+25920000),'/','',0); |
cab08090 |
121 | if ($logger) { |
63528107 |
122 | $logger->log("cookie_on"); |
0337d704 |
123 | } |
63528107 |
124 | } else { |
125 | setcookie('ORGaccess', '', time() - 3600, '/', '', 0); |
cab08090 |
126 | |
63528107 |
127 | if ($logger) { |
128 | $logger->log("cookie_off"); |
0337d704 |
129 | } |
0337d704 |
130 | } |
63528107 |
131 | return true; |
132 | } elseif ($logger) { |
133 | $logger->log('auth_fail','bad password'); |
0337d704 |
134 | } |
63528107 |
135 | } elseif ($logger) { |
136 | $logger->log('auth_fail','bad login'); |
137 | } |
b0b937fd |
138 | |
63528107 |
139 | return false; |
0337d704 |
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 | */ |
f6ce9a88 |
149 | function doAuthCookie() |
0337d704 |
150 | { |
cab08090 |
151 | if (S::logged()) { |
f6ce9a88 |
152 | return true; |
0337d704 |
153 | } |
154 | |
155 | if (Env::has('username') and Env::has('response')) { |
b0b937fd |
156 | return XorgSession::doAuth(); |
0337d704 |
157 | } |
158 | |
159 | if ($r = try_cookie()) { |
b0b937fd |
160 | return XorgSession::doAuth(($r > 0)); |
0337d704 |
161 | } |
f6ce9a88 |
162 | |
163 | return false; |
0337d704 |
164 | } |
165 | |
166 | // }}} |
0337d704 |
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 | { |
5e2307dc |
178 | if (Cookie::v('ORGaccess') == '' or !Cookie::has('ORGuid')) { |
0337d704 |
179 | return -1; |
180 | } |
181 | |
08cce2ff |
182 | $res = @XDB::query( |
0337d704 |
183 | "SELECT user_id,password FROM auth_user_md5 WHERE user_id = {?} AND perms IN('admin','user')", |
5e2307dc |
184 | Cookie::i('ORGuid') |
0337d704 |
185 | ); |
186 | if ($res->numRows() != 0) { |
187 | list($uid, $password) = $res->fetchOneRow(); |
46bde4d1 |
188 | require_once('secure_hash.inc.php'); |
189 | $expected_value = hash_encrypt($password); |
5e2307dc |
190 | if ($expected_value == Cookie::v('ORGaccess')) { |
0337d704 |
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 | { |
08cce2ff |
211 | $res = XDB::query(" |
0337d704 |
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(); |
cab08090 |
224 | $suid = S::v('suid'); |
225 | |
0337d704 |
226 | if ($suid) { |
227 | $logger = new DiogenesCoreLogger($uid, $suid); |
cab08090 |
228 | $logger->log("suid_start", S::v('forlife')." by {$suid['uid']}"); |
0337d704 |
229 | $sess['suid'] = $suid; |
230 | } else { |
cab08090 |
231 | $logger = S::v('log', new DiogenesCoreLogger($uid)); |
0337d704 |
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 | // }}} |
0337d704 |
243 | |
244 | function set_skin() |
245 | { |
246 | global $globals; |
63528107 |
247 | if (S::logged() && !S::has('skin')) { |
cab08090 |
248 | $uid = S::v('uid'); |
63528107 |
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()) { |
0337d704 |
254 | return; |
255 | } |
256 | } |
0337d704 |
257 | } |
258 | |
0337d704 |
259 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: |
260 | ?> |