openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 7ec1b91ab730dadeb5d96dd02e1a4e3c94db6cff
parent ac41d9c9ac0e16e4bad6e257a177ab3e8a5e2225
Author: Jan Dankert <devnull@localhost>
Date:   Mon,  5 Nov 2012 19:24:32 +0100

Initiale Version der Auth-Klassen, vorerst ohne Benutzung.

Diffstat:
Aauth/Auth.class.php | 9+++++++++
Aauth/Database.class.php | 40++++++++++++++++++++++++++++++++++++++++
Aauth/Http.class.php | 252+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aauth/Ident.class.php | 0
Aauth/Internal.class.php | 237+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aauth/Ldap.class.php | 0
Aauth/Personas.class.php | 0
Aauth/include.inc.php | 13+++++++++++++
Minit.php | 1+
Mutil/include.inc.php | 6------
10 files changed, 552 insertions(+), 6 deletions(-)

diff --git a/auth/Auth.class.php b/auth/Auth.class.php @@ -0,0 +1,8 @@ +<?php + +interface Auth +{ + function login( $username, $password ); +} + +?> +\ No newline at end of file diff --git a/auth/Database.class.php b/auth/Database.class.php @@ -0,0 +1,39 @@ +<?php + +/** + * Authentifzierung über eine externe Datenbank. + * @author dankert + * + */ +class DatabaseAuth implements Auth +{ + + /** + * Login. + */ + function login( $user, $password ) + { + global $conf; + + $authdb = new DB( $conf['security']['authdb'] ); + $sql = new Sql( $conf['security']['authdb']['sql'] ); + $sql->setString('username',$this->name); + $sql->setString('password',$password); + $row = $authdb->getRow( $sql ); + $ok = !empty($row); + + if ( $ok && $autoAdd ) + { + // Falls die Authentifizierung geklappt hat, wird der + // Benutzername in der eigenen Datenbank eingetragen. + $this->fullname = $this->name; + $this->add(); + $this->save(); + } + // noch nicht implementiert: $authdb->close(); + + return $ok; + } +} + +?> +\ No newline at end of file diff --git a/auth/Http.class.php b/auth/Http.class.php @@ -0,0 +1,251 @@ +<?php + +class HttpAuth implements Auth +{ + + function username() + { + return null; + } + + + /** + * Ueberpruefen des Kennwortes. + * + * Das Kennwort wird ueber Datenbank oder ueber LDAP-Verzeichnisdienst geprueft. + * Wenn + * - ein LDAP-Dn ("distinghished-name") vorhanden ist, dann Pruefung ueber den LDAP-Server, + * - sonst ueber die Benutzertabelle in der Datenbank. + */ + function login( $user, $password ) + { + global $conf; + + $http = new Http( $conf['security']['http']['url'] ); + $http->method = 'HEAD'; + $http->setBasicAuthentication( $this->name, $password ); + + $ok = $http->request(); + + return $ok; + + + $db = db_connection(); + $this->mustChangePassword = false; + + // Lesen des Benutzers aus der DB-Tabelle + $sql = new Sql( <<<SQL +SELECT * FROM {t_user} + WHERE name={name} +SQL + ); + $sql->setString('name',$this->name); + + $row_user = $db->getRow( $sql ); + + $check = false; + $authType = $conf['security']['auth']['type']; // Entweder 'ldap', 'authdb', 'http', oder 'database' + + if ( !empty($row_user) ) + { + // Benutzername ist bereits in der Datenbank. + $this->userid = $row_user['id']; + $this->ldap_dn = $row_user['ldap_dn']; + $check = true; + $autoAdd = false; // Darf nicht hinzugef�gt werden, da schon vorhanden. + } + elseif( $authType == 'ldap' && $conf['ldap']['search']['add'] ) + { + // Benutzer noch nicht in der Datenbank vorhanden. + // Falls ein LDAP-Account gefunden wird, wird dieser �bernommen. + $check = true; + $autoAdd = true; + } + elseif( $authType == 'authdb' && $conf['security']['authdb']['add'] ) + { + $check = true; + $autoAdd = true; + } + elseif( $authType == 'http' && $conf['security']['http']['add'] ) + { + $check = true; + $autoAdd = true; + } + + if ( $check ) + { + // Falls benutzerspezifischer LDAP-dn vorhanden wird Benutzer per LDAP authentifiziert + if ( $conf['security']['auth']['userdn'] && !empty($this->ldap_dn ) ) + { + Logger::debug( 'checking login via ldap' ); + $ldap = new Ldap(); + $ldap->connect(); + + // Benutzer ist bereits in Datenbank + // LDAP-Login mit dem bereits vorhandenen DN versuchen + $ok = $ldap->bind( $this->ldap_dn, $password ); + + // Verbindung zum LDAP-Server brav beenden + $ldap->close(); + + return $ok; + } + elseif( $authType == 'ldap' ) + { + Logger::debug( 'checking login via ldap' ); + $ldap = new Ldap(); + $ldap->connect(); + + if ( empty($conf['ldap']['dn']) ) + { + // Der Benutzername wird im LDAP-Verzeichnis gesucht. + // Falls gefunden, wird der DN (=der eindeutige Schl�ssel im Verzeichnis) ermittelt. + $dn = $ldap->searchUser( $this->name ); + + if ( empty($dn) ) + { + Logger::debug( 'User not found in LDAP directory' ); + return false; // Kein LDAP-Account gefunden. + } + + Logger::debug( 'User found: '.$dn ); + } + else + { + $dn = str_replace( '{user}',$this->name,$conf['ldap']['dn'] ); + } + + // LDAP-Login versuchen + $ok = $ldap->bind( $dn, $password ); + + Logger::debug( 'LDAP bind: '.($ok?'success':'failed') ); + + if ( $ok && $conf['security']['authorize']['type'] == 'ldap' ) + { + $sucheAttribut = $conf['ldap']['authorize']['group_name']; + $sucheFilter = str_replace('{dn}',$dn,$conf['ldap']['authorize']['group_filter']); + + $ldap_groups = $ldap->searchAttribute( $sucheFilter, $sucheAttribut ); + $sql_ldap_groups = "'".implode("','",$ldap_groups)."'"; + + $sql = new Sql( <<<SQL +SELECT id,name FROM {t_group} + WHERE name IN($sql_ldap_groups) + ORDER BY name ASC +SQL + ); + $oldGroups = $this->getGroupIds(); + $this->groups = $db->getAssoc( $sql ); + + foreach( $this->groups as $groupid=>$groupname) + { + if ( ! in_array($groupid,$oldGroups)) + $this->addGroup($groupid); + } + foreach( $oldGroups as $groupid) + { + if ( !isset($this->groups[$groupid]) ) + $this->delGroup($groupid); + } + + + // Pr�fen, ob Gruppen fehlen. Diese dann ggf. in der OpenRat-Datenbank hinzuf�gen. + if ( $conf['ldap']['authorize']['auto_add'] ) + { + foreach( $ldap_groups as $group ) + { + if ( !in_array($group,$this->groups) ) // Gruppe schon da? + { + $g = new Group(); + $g->name = $group; + $g->add(); // Gruppe hinzuf�gen + + $this->groups[$g->groupid] = $group; + } + } + } +// Html::debug($this->groups,'Gruppen/Ids des Benutzers'); + } + + // Verbindung zum LDAP-Server brav beenden + $ldap->close(); + + if ( $ok && $autoAdd ) + { + // Falls die Authentifizierung geklappt hat, wird der + // LDAP-Account in die Datenbank �bernommen. + $this->ldap_dn = $dn; + $this->fullname = $this->name; + $this->add(); + $this->save(); + } + + return $ok; + } + elseif( $authType == 'database' ) + { + // Pruefen ob Kennwort mit Datenbank uebereinstimmt + if ( $row_user['password'] == $password ) + { + // Kennwort stimmt mit Datenbank �berein, aber nur im Klartext. + // Das Kennwort muss ge�ndert werden + $this->mustChangePassword = true; + + // Login nicht erfolgreich + return false; + } + elseif ( $row_user['password'] == md5( $this->saltPassword($password) ) ) + { + // Die Kennwort-Pr�fsumme stimmt mit dem aus der Datenbank �berein. + // Juchuu, Login ist erfolgreich. + return true; + } + else + { + // Kennwort stimmt garnicht �berein. + return false; + } + } + elseif( $authType == 'authdb' ) + { + $authdb = new DB( $conf['security']['authdb'] ); + $sql = new Sql( $conf['security']['authdb']['sql'] ); + $sql->setString('username',$this->name); + $sql->setString('password',$password); + $row = $authdb->getRow( $sql ); + $ok = !empty($row); + + if ( $ok && $autoAdd ) + { + // Falls die Authentifizierung geklappt hat, wird der + // Benutzername in der eigenen Datenbank eingetragen. + $this->fullname = $this->name; + $this->add(); + $this->save(); + } + // noch nicht implementiert: $authdb->close(); + + return $ok; + } + elseif( $authType == 'http' ) + { + $http = new Http( $conf['security']['http']['url'] ); + $http->method = 'HEAD'; + $http->setBasicAuthentication( $this->name, $password ); + + $ok = $http->request(); + + return $ok; + } + else + { + die( 'unknown authentication-type in configuration: '.$authType ); + } + } + + // Benutzername nicht in Datenbank. + return false; + } +} + +?> +\ No newline at end of file diff --git a/auth/Ident.class.php b/auth/Ident.class.php diff --git a/auth/Internal.class.php b/auth/Internal.class.php @@ -0,0 +1,236 @@ +<?php + +class InternalAuth +{ + + /** + * Ueberpruefen des Kennwortes. + * + * Das Kennwort wird ueber Datenbank oder ueber LDAP-Verzeichnisdienst geprueft. + * Wenn + * - ein LDAP-Dn ("distinghished-name") vorhanden ist, dann Pruefung ueber den LDAP-Server, + * - sonst ueber die Benutzertabelle in der Datenbank. + */ + function checkPassword( $password ) + { + global $conf; + + $db = db_connection(); + $this->mustChangePassword = false; + + // Lesen des Benutzers aus der DB-Tabelle + $sql = new Sql( <<<SQL +SELECT * FROM {t_user} + WHERE name={name} +SQL + ); + $sql->setString('name',$this->name); + + $row_user = $db->getRow( $sql ); + + $check = false; + $authType = $conf['security']['auth']['type']; // Entweder 'ldap', 'authdb', 'http', oder 'database' + + if ( !empty($row_user) ) + { + // Benutzername ist bereits in der Datenbank. + $this->userid = $row_user['id']; + $this->ldap_dn = $row_user['ldap_dn']; + $check = true; + $autoAdd = false; // Darf nicht hinzugef�gt werden, da schon vorhanden. + } + elseif( $authType == 'ldap' && $conf['ldap']['search']['add'] ) + { + // Benutzer noch nicht in der Datenbank vorhanden. + // Falls ein LDAP-Account gefunden wird, wird dieser �bernommen. + $check = true; + $autoAdd = true; + } + elseif( $authType == 'authdb' && $conf['security']['authdb']['add'] ) + { + $check = true; + $autoAdd = true; + } + elseif( $authType == 'http' && $conf['security']['http']['add'] ) + { + $check = true; + $autoAdd = true; + } + + if ( $check ) + { + // Falls benutzerspezifischer LDAP-dn vorhanden wird Benutzer per LDAP authentifiziert + if ( $conf['security']['auth']['userdn'] && !empty($this->ldap_dn ) ) + { + Logger::debug( 'checking login via ldap' ); + $ldap = new Ldap(); + $ldap->connect(); + + // Benutzer ist bereits in Datenbank + // LDAP-Login mit dem bereits vorhandenen DN versuchen + $ok = $ldap->bind( $this->ldap_dn, $password ); + + // Verbindung zum LDAP-Server brav beenden + $ldap->close(); + + return $ok; + } + elseif( $authType == 'ldap' ) + { + Logger::debug( 'checking login via ldap' ); + $ldap = new Ldap(); + $ldap->connect(); + + if ( empty($conf['ldap']['dn']) ) + { + // Der Benutzername wird im LDAP-Verzeichnis gesucht. + // Falls gefunden, wird der DN (=der eindeutige Schl�ssel im Verzeichnis) ermittelt. + $dn = $ldap->searchUser( $this->name ); + + if ( empty($dn) ) + { + Logger::debug( 'User not found in LDAP directory' ); + return false; // Kein LDAP-Account gefunden. + } + + Logger::debug( 'User found: '.$dn ); + } + else + { + $dn = str_replace( '{user}',$this->name,$conf['ldap']['dn'] ); + } + + // LDAP-Login versuchen + $ok = $ldap->bind( $dn, $password ); + + Logger::debug( 'LDAP bind: '.($ok?'success':'failed') ); + + if ( $ok && $conf['security']['authorize']['type'] == 'ldap' ) + { + $sucheAttribut = $conf['ldap']['authorize']['group_name']; + $sucheFilter = str_replace('{dn}',$dn,$conf['ldap']['authorize']['group_filter']); + + $ldap_groups = $ldap->searchAttribute( $sucheFilter, $sucheAttribut ); + $sql_ldap_groups = "'".implode("','",$ldap_groups)."'"; + + $sql = new Sql( <<<SQL +SELECT id,name FROM {t_group} + WHERE name IN($sql_ldap_groups) + ORDER BY name ASC +SQL + ); + $oldGroups = $this->getGroupIds(); + $this->groups = $db->getAssoc( $sql ); + + foreach( $this->groups as $groupid=>$groupname) + { + if ( ! in_array($groupid,$oldGroups)) + $this->addGroup($groupid); + } + foreach( $oldGroups as $groupid) + { + if ( !isset($this->groups[$groupid]) ) + $this->delGroup($groupid); + } + + + // Pr�fen, ob Gruppen fehlen. Diese dann ggf. in der OpenRat-Datenbank hinzuf�gen. + if ( $conf['ldap']['authorize']['auto_add'] ) + { + foreach( $ldap_groups as $group ) + { + if ( !in_array($group,$this->groups) ) // Gruppe schon da? + { + $g = new Group(); + $g->name = $group; + $g->add(); // Gruppe hinzuf�gen + + $this->groups[$g->groupid] = $group; + } + } + } +// Html::debug($this->groups,'Gruppen/Ids des Benutzers'); + } + + // Verbindung zum LDAP-Server brav beenden + $ldap->close(); + + if ( $ok && $autoAdd ) + { + // Falls die Authentifizierung geklappt hat, wird der + // LDAP-Account in die Datenbank �bernommen. + $this->ldap_dn = $dn; + $this->fullname = $this->name; + $this->add(); + $this->save(); + } + + return $ok; + } + elseif( $authType == 'database' ) + { + // Pruefen ob Kennwort mit Datenbank uebereinstimmt + if ( $row_user['password'] == $password ) + { + // Kennwort stimmt mit Datenbank �berein, aber nur im Klartext. + // Das Kennwort muss ge�ndert werden + $this->mustChangePassword = true; + + // Login nicht erfolgreich + return false; + } + elseif ( $row_user['password'] == md5( $this->saltPassword($password) ) ) + { + // Die Kennwort-Pr�fsumme stimmt mit dem aus der Datenbank �berein. + // Juchuu, Login ist erfolgreich. + return true; + } + else + { + // Kennwort stimmt garnicht �berein. + return false; + } + } + elseif( $authType == 'authdb' ) + { + $authdb = new DB( $conf['security']['authdb'] ); + $sql = new Sql( $conf['security']['authdb']['sql'] ); + $sql->setString('username',$this->name); + $sql->setString('password',$password); + $row = $authdb->getRow( $sql ); + $ok = !empty($row); + + if ( $ok && $autoAdd ) + { + // Falls die Authentifizierung geklappt hat, wird der + // Benutzername in der eigenen Datenbank eingetragen. + $this->fullname = $this->name; + $this->add(); + $this->save(); + } + // noch nicht implementiert: $authdb->close(); + + return $ok; + } + elseif( $authType == 'http' ) + { + $http = new Http( $conf['security']['http']['url'] ); + $http->method = 'HEAD'; + $http->setBasicAuthentication( $this->name, $password ); + + $ok = $http->request(); + + return $ok; + } + else + { + die( 'unknown authentication-type in configuration: '.$authType ); + } + } + + // Benutzername nicht in Datenbank. + return false; + } +} + +?> +\ No newline at end of file diff --git a/auth/Ldap.class.php b/auth/Ldap.class.php diff --git a/auth/Personas.class.php b/auth/Personas.class.php diff --git a/auth/include.inc.php b/auth/include.inc.php @@ -0,0 +1,12 @@ +<?php + +require_once( OR_AUTHCLASSES_DIR."Auth.class.".PHP_EXT ); +require_once( OR_AUTHCLASSES_DIR."Database.class.".PHP_EXT ); +require_once( OR_AUTHCLASSES_DIR."Http.class.".PHP_EXT ); +require_once( OR_AUTHCLASSES_DIR."Ident.class.".PHP_EXT ); +require_once( OR_AUTHCLASSES_DIR."Internal.class.".PHP_EXT ); +require_once( OR_AUTHCLASSES_DIR."Ldap.class.".PHP_EXT ); +require_once( OR_AUTHCLASSES_DIR."OpenId.class.".PHP_EXT ); +require_once( OR_AUTHCLASSES_DIR."Personas.class.".PHP_EXT ); + +?> +\ No newline at end of file diff --git a/init.php b/init.php @@ -73,6 +73,7 @@ require_once( "functions/request.inc.php" ); // Werkzeugklassen einbinden. require_once( OR_SERVICECLASSES_DIR."include.inc.".PHP_EXT ); +require_once( OR_AUTHCLASSES_DIR."include.inc.".PHP_EXT ); // TODO: Muss aus Datenbank kommen! $charset = 'US-ASCII'; diff --git a/util/include.inc.php b/util/include.inc.php @@ -20,12 +20,6 @@ require_once( OR_SERVICECLASSES_DIR."JSON.class.".PHP_EXT ); require_once( OR_SERVICECLASSES_DIR."ProjectTree.class.".PHP_EXT ); } -// Login -//if ( !empty($REQ[REQ_PARAM_ACTION]) && in_array($REQ[REQ_PARAM_ACTION],array('index')) ) -{ - require_once( OR_AUTHCLASSES_DIR."OpenId.class.".PHP_EXT ); -} - // Veroeffentlichung //if ( !empty($REQ[REQ_PARAM_ACTION]) && in_array($REQ[REQ_PARAM_ACTION],array('file','page','pageelement','folder')) ) {