certlogin/index.php
18b15c1f
 <?php
c208bd90
 /*
 This file belongs to the Webinterface of schokokeks.org Hosting
 
cf54502a
 Written 2008-2018 by schokokeks.org Hosting, namely
c208bd90
   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.
 
2626dd47
 You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see
c208bd90
 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.
 */
18b15c1f
 
4b480f5b
 // Setze das Arbeitsverzeichnis auf das Stammverzeichnis, damit die Voraussetzungen gleich sind wie bei allen anderen Requests
d73051da
 chdir('..');
 
 require_once('config.php');
18b15c1f
 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');
d73051da
 require_once('inc/theme.php');
8175ea0d
 require_once('modules/index/include/x509.php');
18b15c1f
 
 
100e4c0c
 DEBUG('$_SERVER:');
 DEBUG($_SERVER);
 
6e93bd70
 $redirect = "";
 if (! isset($_SERVER['SSL_CLIENT_CERT']) && isset($_SERVER['REDIRECT_SSL_CLIENT_CERT'])) {
2626dd47
     $redirect = "REDIRECT_";
6e93bd70
 }
18b15c1f
 
2626dd47
 if ($_SESSION['role'] != ROLE_ANONYMOUS && isset($_REQUEST['record']) && isset($_REQUEST['backto']) && check_path($_REQUEST['backto'])) {
     DEBUG('recording client-cert');
     if (isset($_SERVER[$redirect.'SSL_CLIENT_CERT']) && isset($_SERVER[$redirect.'SSL_CLIENT_S_DN']) &&
7ffb2146
       isset($_SERVER[$redirect.'SSL_CLIENT_I_DN']) && isset($_SERVER[$redirect.'SSL_CLIENT_M_SERIAL']) &&
       isset($_SERVER[$redirect.'SSL_CLIENT_V_START']) && isset($_SERVER[$redirect.'SSL_CLIENT_V_END'])
2626dd47
       ) {
         $_SESSION['clientcert_cert'] = $_SERVER[$redirect.'SSL_CLIENT_CERT'];
         $_SESSION['clientcert_dn'] = $_SERVER[$redirect.'SSL_CLIENT_S_DN'];
         $_SESSION['clientcert_issuer'] = $_SERVER[$redirect.'SSL_CLIENT_I_DN'];
         $_SESSION['clientcert_serial'] = $_SERVER[$redirect.'SSL_CLIENT_M_SERIAL'];
         $vstart = new DateTime($_SERVER[$redirect.'SSL_CLIENT_V_START']);
         $_SESSION['clientcert_valid_from'] = date_format($vstart, 'Y-m-d');
         $vend = new DateTime($_SERVER[$redirect.'SSL_CLIENT_V_END']);
         $_SESSION['clientcert_valid_until'] = date_format($vend, 'Y-m-d');
         header('Location: '.$prefix.$_REQUEST['backto'].encode_querystring(''));
         die();
     } else {
         warning('Ihr Browser hat kein Client-Zertifikat gesendet');
         header('Location: '.$prefix.$_REQUEST['backto'].encode_querystring(''));
         die();
     }
 } elseif (isset($_REQUEST['type']) && isset($_REQUEST['username'])) {
     if (!isset($_SERVER[$redirect.'SSL_CLIENT_CERT'])) {
         system_failure('Ihr Browser hat kein Client-Zertifikat gesendet');
     }
18b15c1f
 
2626dd47
     $ret = get_logins_by_cert($_SERVER[$redirect.'SSL_CLIENT_CERT']);
     DEBUG($ret);
     foreach ($ret as $account) {
         DEBUG('/'.$account['type'].'/'.$_REQUEST['type'].'/    /'.$account['username'].'/'.$_REQUEST['username'].'/    =>');
         if (($account['type'] == urldecode($_REQUEST['type'])) && ($account['username'] == urldecode($_REQUEST['username']))) {
             $uid = $account['username'];
             $role = find_role($uid, '', true);
             setup_session($role, $uid);
             $destination = 'go/index/index';
             if (check_path($account['startpage'])) {
                 $destination = $account['startpage'];
             }
             if (isset($_REQUEST['destination']) && check_path($_REQUEST['destination'])) {
                 $destination = $_REQUEST['destination'];
             }
d6d6fcfe
             header('Location: ../'.ltrim($destination, '/'));
2626dd47
             die();
         }
18b15c1f
     }
2626dd47
     system_failure('Der angegebene Account kann mit diesem Client-Zertifikat nicht eingeloggt werden.');
fe7b398d
 } elseif ($_SESSION['role'] != ROLE_ANONYMOUS && isset($_REQUEST['destination']) && $_REQUEST['destination'] != '') {
2626dd47
     # User hat sich grade eingeloggt
d6d6fcfe
     header('Location: ../'.ltrim($destination, '/'));
2626dd47
 } else {
     if (isset($_SERVER[$redirect.'SSL_CLIENT_CERT']) &&
       isset($_SERVER[$redirect.'SSL_CLIENT_S_DN']) && $_SERVER[$redirect.'SSL_CLIENT_S_DN'] != '' &&
84f379c0
       isset($_SERVER[$redirect.'SSL_CLIENT_I_DN']) && $_SERVER[$redirect.'SSL_CLIENT_I_DN'] != '' &&
       isset($_SERVER[$redirect.'SSL_CLIENT_M_SERIAL']) && $_SERVER[$redirect.'SSL_CLIENT_M_SERIAL'] != '') {
2626dd47
         $ret = get_logins_by_cert($_SERVER[$redirect.'SSL_CLIENT_CERT']);
         if ($ret === null) {
             login_screen('Ihr Browser hat ein Client-Zertifikat gesendet, dieses ist aber noch nicht für den Zugang hinterlegt. Melden Sie sich bitte per Benutzername und Passwort an.');
         }
         if (count($ret) == 1) {
             $uid = $ret[0]['username'];
             $role = find_role($uid, '', true);
             setup_session($role, $uid);
             DEBUG("Set Cookie!");
             setcookie('CLIENTCERT_AUTOLOGIN', '1', strtotime("+ 1 year"), '/', '', true, true);
             $destination = 'go/index/index';
             if (check_path($ret[0]['startpage'])) {
                 $destination = $ret[0]['startpage'];
             }
             if (isset($_REQUEST['destination']) && check_path($_REQUEST['destination'])) {
                 $destination = $_REQUEST['destination'];
             }
d6d6fcfe
             header('Location: ../'.ltrim($destination, '/'));
2626dd47
             die();
         }
         output('<p>Ihr Browser hat ein gültiges SSL-Client-Zertifikat gesendet, mit dem Sie sich auf dieser Seite einloggen können. Allerdings haben Sie dieses Client-Zertifikat für mehrere Zugänge hinterlegt. Wählen Sie bitte den Zugang aus, mit dem Sie sich anmelden möchten.</p>
18b15c1f
       <ul>');
2626dd47
         foreach ($ret as $account) {
             $type = 'System-Account';
             if ($account['type'] == 'email') {
                 $type = 'E-Mail-Konto';
             } elseif ($account['type'] == 'subuser') {
                 $type = 'Unter-Nutzer';
             } elseif ($account['type'] == 'customer') {
                 $type = 'Kundenaccount';
             }
             $destination = 'go/index/index';
             if ($account['startpage'] && check_path($account['startpage'])) {
                 $destination = $account['startpage'];
             }
             output('<li>'.internal_link('', $type.': <strong>'.$account['username'].'</strong>', 'type='.$account['type'].'&username='.urlencode($account['username']).'&destination='.urlencode($destination)).'</li>');
         }
         output('</ul>');
     } else {
         warning('Ihr Browser hat kein Client-Zertifikat gesendet. Eventuell müssen Sie in den Einstellungen des Browsers diese Funktion einschalten.');
         redirect('/');
         die();
18b15c1f
     }
 }
 
d73051da
 show_page('certlogin');