Merge branch 'fusionax' into account
[platal.git] / modules / auth.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 AuthModule extends PLModule
23 {
24 function handlers()
25 {
26 return array(
27 'groupex/donne-chall.php'
28 => $this->make_hook('chall', AUTH_PUBLIC),
29 'groupex/export-econfiance.php'
30 => $this->make_hook('econf', AUTH_PUBLIC, 'user', NO_HTTPS),
31
32 'webservices/manageurs.php'
33 => $this->make_hook('manageurs', AUTH_PUBLIC, 'user', NO_HTTPS),
34
35 'auth-redirect.php' => $this->make_hook('redirect', AUTH_COOKIE),
36 'auth-groupex.php' => $this->make_hook('groupex_old', AUTH_COOKIE),
37 'auth-groupex' => $this->make_hook('groupex', AUTH_COOKIE),
38 'admin/auth-groupes-x' => $this->make_hook('admin_authgroupesx', AUTH_MDP, 'admin'),
39 );
40 }
41
42 function handler_chall(&$page)
43 {
44 $_SESSION["chall"] = uniqid(rand(), 1);
45 echo $_SESSION["chall"] . "\n" . session_id();
46 exit;
47 }
48
49 function handler_econf(&$page)
50 {
51 global $globals;
52
53 $cle = $globals->core->econfiance;
54
55 $res = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<membres>\n\n";
56
57 if (S::v('chall') && $_GET['PASS'] == md5(S::v('chall').$cle)) {
58 $list = new MMList(User::getWithUID(10154), "x-econfiance.polytechnique.org");
59 $members = $list->get_members('membres');
60 if (is_array($members)) {
61 $membres = Array();
62 foreach($members[1] as $member) {
63 $user = User::getSilent($member[1]);
64 if ($user && $user->hasProfile()) {
65 $profile = $user->profile();
66 $res .= "<membre>\n";
67 $res .= "\t<nom>" . $profile->lastName() . "</nom>\n";
68 $res .= "\t<prenom>" . $profile->firstName() . "</prenom>\n";
69 $res .= "\t<email>" . $user->forlifeEmail() . "</email>\n";
70 $res .= "</membre>\n\n";
71 }
72 }
73 }
74 $res .= "</membres>\n\n";
75
76 header('Content-Type: text/xml; charset="UTF-8"');
77 echo $res;
78 }
79 exit;
80 }
81
82 function handler_manageurs(&$page)
83 {
84 global $globals;
85
86 require_once 'webservices/manageurs.server.inc.php';
87
88 $ips = array_flip(explode(' ', $globals->manageurs->authorized_ips));
89 if ($ips && isset($ips[$_SERVER['REMOTE_ADDR']])) {
90 $server = xmlrpc_server_create();
91
92 xmlrpc_server_register_method($server, 'get_annuaire_infos', 'get_annuaire_infos');
93 xmlrpc_server_register_method($server, 'get_nouveau_infos', 'get_nouveau_infos');
94
95 $request = @$GLOBALS['HTTP_RAW_POST_DATA'];
96 $response = xmlrpc_server_call_method($server, $request, null);
97 header('Content-Type: text/xml');
98 print $response;
99 xmlrpc_server_destroy($server);
100 }
101
102 exit;
103 }
104
105 function handler_redirect(&$page)
106 {
107 http_redirect(Env::v('dest', '/'));
108 }
109
110 function handler_groupex_old(&$page)
111 {
112 return $this->handler_groupex($page, 'iso-8859-1');
113 }
114
115 function handler_groupex(&$page, $charset = 'utf8')
116 {
117 $this->load('auth.inc.php');
118 $page->assign('referer', true);
119
120 $gpex_pass = $_GET["pass"];
121 $gpex_url = urldecode($_GET["url"]);
122 if (strpos($gpex_url, '?') === false) {
123 $gpex_url .= "?PHPSESSID=" . $_GET["session"];
124 } else {
125 $gpex_url .= "&PHPSESSID=" . $_GET["session"];
126 }
127
128 /* a-t-on besoin d'ajouter le http:// ? */
129 if (!preg_match("/^(http|https):\/\/.*/",$gpex_url)) {
130 $gpex_url = "http://$gpex_url";
131 }
132 $gpex_challenge = $_GET["challenge"];
133
134 // mise à jour de l'heure et de la machine de dernier login sauf quand on est en suid
135 $uid = S::i('uid');
136 if (!S::suid()) {
137 global $platal;
138 S::logger($uid)->log('connexion_auth_ext', $platal->path);
139 }
140
141 /* on parcourt les entrees de groupes_auth */
142 $res = XDB::iterRow('SELECT privkey, name, datafields, returnurls FROM groupesx_auth');
143
144 while (list($privkey,$name,$datafields,$returnurls) = $res->next()) {
145 if (md5($gpex_challenge.$privkey) == $gpex_pass) {
146 $returnurls = trim($returnurls);
147 if (empty($returnurls) || @preg_match($returnurls, $gpex_url)) {
148 $returl = $gpex_url . gpex_make_params($gpex_challenge, $privkey, $datafields, $charset);
149 http_redirect($returl);
150 }
151 }
152 }
153
154 /* si on n'a pas trouvé, on renvoit sur x.org */
155 http_redirect('https://www.polytechnique.org/');
156 }
157
158 function handler_admin_authgroupesx(&$page, $action = 'list', $id = null)
159 {
160 $page->setTitle('Administration - Auth groupes X');
161 $page->assign('title', 'Gestion de l\'authentification centralisée');
162 $table_editor = new PLTableEditor('admin/auth-groupes-x','groupesx_auth','id');
163 $table_editor->describe('name','nom',true);
164 $table_editor->describe('privkey','clé privée',false);
165 $table_editor->describe('datafields','champs renvoyés',true);
166 $table_editor->describe('returnurls','urls de retour',true);
167 $table_editor->apply($page, $action, $id);
168 }
169 }
170
171 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
172 ?>