certlogin/ajax.php
4b480f5b
 <?php
 /*
 This file belongs to the Webinterface of schokokeks.org Hosting
 
5effc2bd
 Written 2008-2014 by schokokeks.org Hosting, namely
4b480f5b
   Bernd Wurst <bernd@schokokeks.org>
   Hanno Böck <hanno@schokokeks.org>
 
 To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
 
 You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see 
 http://creativecommons.org/publicdomain/zero/1.0/
 
 Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
 */
 
 // Setze das Arbeitsverzeichnis auf das Stammverzeichnis, damit die Voraussetzungen gleich sind wie bei allen anderen Requests
 chdir('..');
 
 require_once('config.php');
 global $prefix;
 $prefix = '../';
 
 // Das Parent-Verzeichnis in den Include-Pfad, da wir uns jetzt in einem anderen Verzeichnis befinden.
 ini_set('include_path', ini_get('include_path').':../');
 
 require_once('session/start.php');
 require_once('inc/base.php');
 require_once('inc/debug.php');
 require_once('inc/error.php');
 require_once('inc/theme.php');
 
 
 function prepare_cert($cert)
 {
 	return str_replace(array('-----BEGIN CERTIFICATE-----', '-----END CERTIFICATE-----', ' ', "\n"), array(), $cert);
 }
 
 
 function get_logins_by_cert($cert) 
 {
f4a55204
 	$cert = prepare_cert($cert);
 	$result = db_query("SELECT type,username,startpage FROM system.clientcert WHERE cert=?", array($cert));
f1f231f5
 	if ($result->rowCount() < 1)
4b480f5b
 		return NULL;
 	else {
 		$ret = array();
f1f231f5
 		while ($row = $result->fetch()) {
4b480f5b
 			$ret[] = $row;
 		}
 		return $ret;
 	}
 }
 
 DEBUG('$_SERVER:');
 DEBUG($_SERVER);
 
 header('Content-type: text/plain');
 
 if (isset($_SERVER['REDIRECT_SSL_CLIENT_CERT']) && 
     isset($_SERVER['REDIRECT_SSL_CLIENT_S_DN']) && $_SERVER['REDIRECT_SSL_CLIENT_S_DN'] != '' && 
     isset($_SERVER['REDIRECT_SSL_CLIENT_I_DN']) && $_SERVER['REDIRECT_SSL_CLIENT_I_DN'] != '') {
   $ret = get_logins_by_cert($_SERVER['REDIRECT_SSL_CLIENT_CERT']);
   if ($ret === NULL) {
     echo 'error';
     die();
   }
   if (count($ret) == 1) {
     $uid = $ret[0]['username'];
     $role = find_role($uid, '', True);
     setup_session($role, $uid);
73aefd48
     setcookie('CLIENTCERT_AUTOLOGIN', '1', time()+3600*24*365, '/');
4b480f5b
     echo 'ok';
     die();
   }
 }
 echo 'error';
 die();
 ?>