add class KerberosApacheAuth and example code

This commit is contained in:
C1-10P 2018-07-20 17:59:03 +02:00 committed by Robin Appelman
commit e8df1df9a2
3 changed files with 113 additions and 46 deletions

View file

@ -27,16 +27,6 @@ use Icewind\SMB\Exception\Exception;
* Use existing kerberos ticket to authenticate
*/
class KerberosAuth implements IAuth {
private $ticketPath = "";
//not working with nextcloud
private $saveTicketInMemory = false;
public function __construct() {
$this->registerApacheKerberosTicket();
}
public function getUsername(): ?string {
return 'dummy';
}
@ -61,40 +51,4 @@ class KerberosAuth implements IAuth {
throw new Exception("Failed to set smbclient options for kerberos auth");
}
}
private function registerApacheKerberosTicket() {
// inspired by https://git.typo3.org/TYPO3CMS/Extensions/fal_cifs.git
if (!extension_loaded("krb5")) {
return;
}
//read apache kerberos ticket cache
$cacheFile = getenv("KRB5CCNAME");
if(!$cacheFile) {
return;
}
$krb5 = new \KRB5CCache();
$krb5->open($cacheFile);
if(!$krb5->isValid()) {
return;
}
if($this->saveTicketInMemory) {
putenv("KRB5CCNAME=" . $krb5->getName());
}
else {
//workaround: smbclient is not working with the original apache ticket cache.
$tmpFilename = tempnam("/tmp", "krb5cc_php_");
$tmpCacheFile = "FILE:" . $tmpFilename;
$krb5->save($tmpCacheFile);
$this->ticketPath = $tmpFilename;
putenv("KRB5CCNAME=" . $tmpCacheFile);
}
}
public function __destruct() {
if(!empty($this->ticketPath) && file_exists($this->ticketPath) && is_file($this->ticketPath)) {
unlink($this->ticketPath);
}
}
}