Commit | Line | Data |
---|---|---|
5d292fd8 | 1 | <?php |
2 | /*************************************************************************** | |
12262f13 | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
5d292fd8 | 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 AuthModule extends PLModule | |
23 | { | |
24 | function handlers() | |
25 | { | |
26 | return array( | |
eb5a266d SJ |
27 | 'groupex/donne-chall.php' => $this->make_hook('chall', AUTH_PUBLIC), |
28 | 'groupex/export-econfiance.php' => $this->make_hook('econf', AUTH_PUBLIC, 'user', NO_HTTPS), | |
d5749a28 | 29 | |
eb5a266d | 30 | 'webservices/manageurs.php' => $this->make_hook('manageurs', AUTH_PUBLIC, 'user', NO_HTTPS), |
87221416 | 31 | |
e5ceaa8c RB |
32 | 'auth-redirect.php' => $this->make_hook('redirect', AUTH_COOKIE, 'user'), |
33 | 'auth-groupex.php' => $this->make_hook('groupex_old', AUTH_COOKIE, 'user'), | |
c1a0ab30 | 34 | 'auth-groupex' => $this->make_hook('groupex', AUTH_PUBLIC), |
bfe9f4c7 | 35 | 'admin/auth-groupes-x' => $this->make_hook('admin_authgroupesx', AUTH_PASSWD, 'admin'), |
5d292fd8 | 36 | ); |
37 | } | |
38 | ||
26ba053e | 39 | function handler_chall($page) |
d5749a28 | 40 | { |
41 | $_SESSION["chall"] = uniqid(rand(), 1); | |
42 | echo $_SESSION["chall"] . "\n" . session_id(); | |
43 | exit; | |
44 | } | |
45 | ||
26ba053e | 46 | function handler_econf($page) |
d5749a28 | 47 | { |
48 | global $globals; | |
49 | ||
d5749a28 | 50 | $cle = $globals->core->econfiance; |
51 | ||
94b72319 FB |
52 | $res = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<membres>\n\n"; |
53 | ||
ed06daba | 54 | if (S::v('chall') && Get::s('PASS') == md5(S::v('chall').$cle)) { |
7f1ff426 | 55 | $list = new MMList(User::getWithUID(10154), "x-econfiance.polytechnique.org"); |
9bb8bf21 | 56 | $members = $list->get_members('membres'); |
d5749a28 | 57 | if (is_array($members)) { |
58 | $membres = Array(); | |
59 | foreach($members[1] as $member) { | |
94b72319 FB |
60 | $user = User::getSilent($member[1]); |
61 | if ($user && $user->hasProfile()) { | |
62 | $profile = $user->profile(); | |
63 | $res .= "<membre>\n"; | |
64 | $res .= "\t<nom>" . $profile->lastName() . "</nom>\n"; | |
65 | $res .= "\t<prenom>" . $profile->firstName() . "</prenom>\n"; | |
66 | $res .= "\t<email>" . $user->forlifeEmail() . "</email>\n"; | |
67 | $res .= "</membre>\n\n"; | |
d5749a28 | 68 | } |
69 | } | |
70 | } | |
d5749a28 | 71 | $res .= "</membres>\n\n"; |
72 | ||
3cb500d5 | 73 | pl_content_headers("text/xml"); |
d5749a28 | 74 | echo $res; |
75 | } | |
76 | exit; | |
77 | } | |
78 | ||
26ba053e | 79 | function handler_manageurs($page) |
87221416 | 80 | { |
81 | global $globals; | |
82 | ||
83 | require_once 'webservices/manageurs.server.inc.php'; | |
84 | ||
34c8c3b5 | 85 | $ips = array_flip(explode(' ', $globals->manageurs->authorized_ips)); |
87221416 | 86 | if ($ips && isset($ips[$_SERVER['REMOTE_ADDR']])) { |
87 | $server = xmlrpc_server_create(); | |
88 | ||
89 | xmlrpc_server_register_method($server, 'get_annuaire_infos', 'get_annuaire_infos'); | |
90 | xmlrpc_server_register_method($server, 'get_nouveau_infos', 'get_nouveau_infos'); | |
91 | ||
206e75c3 | 92 | $request = @$GLOBALS['HTTP_RAW_POST_DATA']; |
87221416 | 93 | $response = xmlrpc_server_call_method($server, $request, null); |
3cb500d5 | 94 | pl_content_headers("text/xml"); |
87221416 | 95 | print $response; |
96 | xmlrpc_server_destroy($server); | |
97 | } | |
34c8c3b5 | 98 | |
99 | exit; | |
87221416 | 100 | } |
101 | ||
26ba053e | 102 | function handler_redirect($page) |
5d292fd8 | 103 | { |
5e2307dc | 104 | http_redirect(Env::v('dest', '/')); |
5d292fd8 | 105 | } |
106 | ||
26ba053e | 107 | function handler_groupex_old($page) |
670b0ac0 | 108 | { |
109 | return $this->handler_groupex($page, 'iso-8859-1'); | |
110 | } | |
111 | ||
d68646f1 RB |
112 | /** Handles the 'auth-groupe-x' authentication. |
113 | * Expects the following GET parameters: | |
114 | * - pass: the 'password' for the authentication | |
115 | * - challenge: the authentication challenge | |
116 | * - url: the return URL | |
117 | * - session: the remote PHP session ID | |
118 | */ | |
26ba053e | 119 | function handler_groupex($page, $charset = 'utf8') |
5d292fd8 | 120 | { |
c1a0ab30 RB |
121 | if (!S::logged()) { |
122 | $page->assign('referer', true); | |
bdbe352c RB |
123 | $page->setTitle('Authentification'); |
124 | $page->setDefaultSkin('group_login'); | |
125 | ||
126 | if (Get::has('group')) { | |
127 | $res = XDB::query('SELECT nom | |
128 | FROM groups | |
129 | WHERE diminutif = {?}', Get::s('group')); | |
130 | $page->assign('group', $res->fetchOneCell()); | |
131 | } else { | |
132 | $page->assign('group', null); | |
133 | } | |
c1a0ab30 RB |
134 | return PL_DO_AUTH; |
135 | } | |
136 | ||
460d8f55 | 137 | $this->load('auth.inc.php'); |
5d292fd8 | 138 | |
d68646f1 RB |
139 | $gpex_pass = Get::s('pass'); |
140 | $gpex_url = urldecode(Get::s('url')); | |
141 | if (Get::has('session')) { | |
142 | if (strpos($gpex_url, '?') === false) { | |
143 | $gpex_url .= "?PHPSESSID=" . Get::s('session'); | |
144 | } else { | |
145 | $gpex_url .= "&PHPSESSID=" . Get::s('session'); | |
146 | } | |
5d292fd8 | 147 | } |
148 | ||
e168b75d | 149 | // Normalize the return URL. |
670b0ac0 | 150 | if (!preg_match("/^(http|https):\/\/.*/",$gpex_url)) { |
5d292fd8 | 151 | $gpex_url = "http://$gpex_url"; |
670b0ac0 | 152 | } |
d68646f1 | 153 | $gpex_challenge = Get::s('challenge'); |
5d292fd8 | 154 | |
e168b75d | 155 | // Update the last login information (unless the user is in SUID). |
0469a7fb | 156 | $uid = S::i('uid'); |
0c02607e | 157 | if (!S::suid()) { |
07e8e0ab | 158 | global $platal; |
d2f8addb | 159 | S::logger($uid)->log('connexion_auth_ext', $platal->path.' '.urldecode($_GET['url'])); |
5d292fd8 | 160 | } |
161 | ||
bd83728a | 162 | if (Get::has('group')) { |
e7737284 | 163 | $req_group_id = XDB::fetchOneCell('SELECT id |
bd83728a RB |
164 | FROM groups |
165 | WHERE diminutif = {?}', | |
166 | Get::s('group')); | |
167 | } else { | |
168 | $req_group_id = null; | |
169 | } | |
170 | ||
e168b75d | 171 | // Iterate over the auth token to find which one did sign the request. |
b8de879d RB |
172 | $res = XDB::iterRow( |
173 | 'SELECT ga.privkey, ga.name, ga.datafields, ga.returnurls, | |
bd83728a | 174 | ga.group_id, ga.flags, g.nom |
b8de879d RB |
175 | FROM group_auth AS ga |
176 | LEFT JOIN groups AS g ON (g.id = ga.group_id)'); | |
177 | ||
bd83728a | 178 | while (list($privkey, $name, $datafields, $returnurls, $group_id, $group_flags, $group_name) = $res->next()) { |
5d292fd8 | 179 | if (md5($gpex_challenge.$privkey) == $gpex_pass) { |
50579de4 | 180 | $returnurls = trim($returnurls); |
e168b75d VZ |
181 | // We check that the return url matches a per-key regexp to prevent |
182 | // replay attacks (more exactly to force replay attacks to redirect | |
183 | // the user to the real GroupeX website, which defeats the attack). | |
50579de4 | 184 | if (empty($returnurls) || @preg_match($returnurls, $gpex_url)) { |
e10bc2ef | 185 | $returl = $gpex_url . gpex_make_params($gpex_challenge, $privkey, $datafields, $charset); |
168a0eda SJ |
186 | XDB::execute('UPDATE group_auth |
187 | SET last_used = DATE(NOW()) | |
188 | WHERE name = {?}', | |
189 | $name); | |
b8de879d RB |
190 | |
191 | // Two conditions control access to the return URL: | |
192 | // - If that URL is attached to a group: | |
193 | // - If the user belongs to the group, OK | |
194 | // - If the user is 'xnet' and doesn't belong, NOK | |
195 | // - If the user is not 'xnet' and the group is not 'strict', OK | |
196 | // - If the user is not 'xnet' and the group is 'strict', NOK | |
197 | // - Otherwise, all but 'xnet' accounts may access the URL. | |
198 | $user_is_xnet = S::user()->type == 'xnet'; | |
bd83728a RB |
199 | $group_flags = new PlFlagSet($group_flags); |
200 | ||
201 | // If this key is not attached to a group, but a group was | |
202 | // requested (e.g query from wiki / blogs / ...), use the | |
203 | // requested group_id. | |
204 | if (!$group_id && $req_group_id) { | |
205 | $group_id = $req_group_id; | |
206 | } | |
b8de879d RB |
207 | |
208 | if ($group_id) { | |
209 | // Check group permissions | |
210 | $is_member = XDB::fetchOneCell('SELECT COUNT(*) | |
211 | FROM group_members | |
212 | WHERE uid = {?} AND asso_id = {?}', | |
213 | S::user()->id(), $group_id); | |
bd83728a | 214 | if (!$is_member && ($user_is_xnet || $group_flags->hasFlag('group_only'))) { |
b8de879d RB |
215 | $page->kill("Le site demandé est réservé aux membres du groupe $group_name."); |
216 | } | |
217 | ||
bd83728a | 218 | } else if ($user_is_xnet && !$group_flags->hasFlag('allow_xnet')) { |
b8de879d RB |
219 | $page->kill("Le site demandé est réservé aux polytechniciens."); |
220 | } | |
221 | ||
e10bc2ef | 222 | http_redirect($returl); |
e168b75d VZ |
223 | } else if (S::admin()) { |
224 | $page->kill("La requête d'authentification a échouée (url de retour invalide)."); | |
e10bc2ef | 225 | } |
5d292fd8 | 226 | } |
227 | } | |
228 | ||
e168b75d VZ |
229 | // Otherwise (if no valid request was found, or if the return URL is not |
230 | // acceptable), the user is redirected back to our homepage. | |
231 | pl_redirect('/'); | |
5d292fd8 | 232 | } |
670b0ac0 | 233 | |
26ba053e | 234 | function handler_admin_authgroupesx($page, $action = 'list', $id = null) |
670b0ac0 | 235 | { |
46f272fe | 236 | $page->setTitle('Administration - Auth groupes X'); |
a7de4ef7 | 237 | $page->assign('title', 'Gestion de l\'authentification centralisée'); |
eb41eda9 | 238 | $table_editor = new PLTableEditor('admin/auth-groupes-x','group_auth','id'); |
92423144 | 239 | $table_editor->describe('name','nom',true); |
47f51789 | 240 | $table_editor->describe('privkey','clé privée',false, true); |
a7de4ef7 | 241 | $table_editor->describe('datafields','champs renvoyés',true); |
e10bc2ef | 242 | $table_editor->describe('returnurls','urls de retour',true); |
168a0eda | 243 | $table_editor->describe('last_used', 'dernière utilisation', true); |
92423144 | 244 | $table_editor->apply($page, $action, $id); |
eaf30d86 | 245 | } |
5d292fd8 | 246 | } |
247 | ||
a7de4ef7 | 248 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
5d292fd8 | 249 | ?> |