Backports
[platal.git] / modules / auth.php
CommitLineData
5d292fd8 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 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
22class AuthModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
4c745880 27 'groupex/donne-chall.php'
87221416 28 => $this->make_hook('chall', AUTH_PUBLIC),
29 'groupex/export-econfiance.php'
30 => $this->make_hook('econf', AUTH_PUBLIC),
d5749a28 31
87221416 32 'webservices/manageurs.php'
33 => $this->make_hook('manageurs', AUTH_PUBLIC),
34
35 'auth-redirect.php' => $this->make_hook('redirect', AUTH_COOKIE),
36 'auth-groupex.php' => $this->make_hook('groupex', AUTH_COOKIE),
92423144 37 'admin/auth-groupes-x' => $this->make_hook('admin_authgroupesx', AUTH_MDP, 'admin'),
5d292fd8 38 );
39 }
40
c9178c75 41 function handler_chall(&$page)
d5749a28 42 {
43 $_SESSION["chall"] = uniqid(rand(), 1);
44 echo $_SESSION["chall"] . "\n" . session_id();
45 exit;
46 }
47
c9178c75 48 function handler_econf(&$page)
d5749a28 49 {
50 global $globals;
51
d5749a28 52 $cle = $globals->core->econfiance;
53
cab08090 54 if (S::v('chall') && $_GET['PASS'] == md5(S::v('chall').$cle)) {
d5749a28 55
08cce2ff 56 $res = XDB::query("SELECT password FROM auth_user_md5 WHERE user_id=10154");
d5749a28 57 $pass = $res->fetchOneCell();
58
9bb8bf21 59 $list = new MMList(10154, $pass, "x-econfiance.polytechnique.org");
60 $members = $list->get_members('membres');
d5749a28 61 if (is_array($members)) {
62 $membres = Array();
63 foreach($members[1] as $member) {
64 if (preg_match('/^([^.]*.[^.]*.(\d\d\d\d))@polytechnique.org$/',
65 $member[1], $matches))
66 {
67 $membres[] = "a.alias='{$matches[1]}'";
68 }
69 }
70 }
71
72 $where = join(' OR ',$membres);
73
08cce2ff 74 $all = XDB::iterRow(
d5749a28 75 "SELECT u.prenom,u.nom,a.alias
76 FROM auth_user_md5 AS u
77 INNER JOIN aliases AS a ON ( u.user_id = a.id AND a.type!='homonyme' )
78 WHERE $where
79 ORDER BY nom");
80
81 $res = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n<membres>\n\n";
82
83 while (list ($prenom1,$nom1,$email1) = $all->next()) {
84 $res .= "<membre>\n";
85 $res .= "\t<nom>$nom1</nom>\n";
86 $res .= "\t<prenom>$prenom1</prenom>\n";
87 $res .= "\t<email>$email1</email>\n";
88 $res .= "</membre>\n\n";
89 }
90
91 $res .= "</membres>\n\n";
92
4e9a4346 93 header('Content-Type: text/xml; charset="ISO-8859-1"');
d5749a28 94 echo $res;
95 }
96 exit;
97 }
98
c9178c75 99 function handler_manageurs(&$page)
87221416 100 {
101 global $globals;
102
103 require_once 'webservices/manageurs.server.inc.php';
104
34c8c3b5 105 $ips = array_flip(explode(' ', $globals->manageurs->authorized_ips));
87221416 106 if ($ips && isset($ips[$_SERVER['REMOTE_ADDR']])) {
107 $server = xmlrpc_server_create();
108
109 xmlrpc_server_register_method($server, 'get_annuaire_infos', 'get_annuaire_infos');
110 xmlrpc_server_register_method($server, 'get_nouveau_infos', 'get_nouveau_infos');
111
112 $request = $GLOBALS['HTTP_RAW_POST_DATA'];
113 $response = xmlrpc_server_call_method($server, $request, null);
114 header('Content-Type: text/xml');
115 print $response;
116 xmlrpc_server_destroy($server);
117 }
34c8c3b5 118
119 exit;
87221416 120 }
121
5d292fd8 122 function handler_redirect(&$page)
123 {
5e2307dc 124 http_redirect(Env::v('dest', '/'));
5d292fd8 125 }
126
127 function handler_groupex(&$page)
128 {
352fb101 129 require_once dirname(__FILE__).'/auth/auth.inc.php';
6c85148b 130 $page->assign('referer', true);
5d292fd8 131
132 $gpex_pass = $_GET["pass"];
133 $gpex_url = urldecode($_GET["url"]);
134 if (strpos($gpex_url, '?') === false) {
135 $gpex_url .= "?PHPSESSID=" . $_GET["session"];
136 } else {
137 $gpex_url .= "&PHPSESSID=" . $_GET["session"];
138 }
139
140 /* a-t-on besoin d'ajouter le http:// ? */
141 if (!preg_match("/^(http|https):\/\/.*/",$gpex_url))
142 $gpex_url = "http://$gpex_url";
143 $gpex_challenge = $_GET["challenge"];
144
089a5801 145 // mise à jour de l'heure et de la machine de dernier login sauf quand on est en suid
5d292fd8 146 if (!isset($_SESSION['suid'])) {
147 $logger = (isset($_SESSION['log']) && $_SESSION['log']->uid == $uid)
07e8e0ab 148 ? $_SESSION['log'] : new CoreLogger($uid);
149 global $platal;
150 $logger->log('connexion_auth_ext', $platal->path);
5d292fd8 151 }
152
153 /* on parcourt les entrees de groupes_auth */
08cce2ff 154 $res = XDB::iterRow('select privkey,name,datafields from groupesx_auth');
5d292fd8 155
156 while (list($privkey,$name,$datafields) = $res->next()) {
157 if (md5($gpex_challenge.$privkey) == $gpex_pass) {
158 $returl = $gpex_url.gpex_make_params($gpex_challenge,$privkey,$datafields);
8b00e0e0 159 http_redirect($returl);
5d292fd8 160 }
161 }
162
089a5801 163 /* si on n'a pas trouvé, on renvoit sur x.org */
8b00e0e0 164 http_redirect('https://www.polytechnique.org/');
5d292fd8 165 }
92423144 166 function handler_admin_authgroupesx(&$page, $action = 'list', $id = null) {
92423144 167 $page->assign('xorg_title','Polytechnique.org - Administration - Auth groupes X');
168 $page->assign('title', 'Gestion de l\'authentification centralisée');
169 $table_editor = new PLTableEditor('admin/auth-groupes-x','groupesx_auth','id');
170 $table_editor->describe('name','nom',true);
171 $table_editor->describe('privkey','clé privée',false);
172 $table_editor->describe('datafields','champs renvoyés',true);
173 $table_editor->apply($page, $action, $id);
174 }
5d292fd8 175}
176
177?>