-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow saving/loading of kerberos tickets
- Loading branch information
1 parent
f944fae
commit a103d9d
Showing
7 changed files
with
186 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* @copyright Copyright (c) 2022 Robin Appelman <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace Icewind\SMB\Exception; | ||
|
||
class InvalidTicket extends Exception { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* @copyright Copyright (c) 2022 Robin Appelman <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace Icewind\SMB; | ||
|
||
use Icewind\SMB\Exception\InvalidTicket; | ||
use KRB5CCache; | ||
|
||
class KerberosTicket { | ||
/** @var KRB5CCache */ | ||
private $krb5; | ||
/** @var string */ | ||
private $cacheName; | ||
|
||
public function __construct(KRB5CCache $krb5, string $cacheName) { | ||
$this->krb5 = $krb5; | ||
$this->cacheName = $cacheName; | ||
} | ||
|
||
public function getCacheName(): string { | ||
return $this->cacheName; | ||
} | ||
|
||
public function getName(): string{ | ||
return $this->krb5->getName(); | ||
} | ||
|
||
public function isValid(): bool { | ||
return count($this->krb5->getEntries()) > 0; | ||
} | ||
|
||
public function validate(): void { | ||
if (!$this->isValid()) { | ||
throw new InvalidTicket("No kerberos ticket found."); | ||
} | ||
} | ||
|
||
/** | ||
* Load the ticket from the cache specified by the KRB5CCNAME variable. | ||
* | ||
* @return KerberosTicket|null | ||
*/ | ||
public static function fromEnv(): ?KerberosTicket { | ||
$ticketName = getenv("KRB5CCNAME"); | ||
if (!$ticketName) { | ||
return null; | ||
} | ||
$krb5 = new KRB5CCache(); | ||
$krb5->open($ticketName); | ||
return new KerberosTicket($krb5, $ticketName); | ||
} | ||
|
||
public static function load(string $ticket): KerberosTicket { | ||
$tmpFilename = tempnam(sys_get_temp_dir(), "krb5cc_php_"); | ||
file_put_contents($tmpFilename, $ticket); | ||
register_shutdown_function(function () use ($tmpFilename) { | ||
if (file_exists($tmpFilename)) { | ||
unlink($tmpFilename); | ||
} | ||
}); | ||
|
||
$ticketName = "FILE:" . $tmpFilename; | ||
$krb5 = new KRB5CCache(); | ||
$krb5->open($ticketName); | ||
return new KerberosTicket($krb5, $ticketName); | ||
} | ||
|
||
public function save(): string { | ||
if (substr($this->cacheName, 0, 5) === 'FILE:') { | ||
$ticket = file_get_contents(substr($this->cacheName, 5)); | ||
} else { | ||
$tmpFilename = tempnam(sys_get_temp_dir(), "krb5cc_php_"); | ||
$tmpCacheFile = "FILE:" . $tmpFilename; | ||
$this->krb5->save($tmpCacheFile); | ||
$ticket = file_get_contents($tmpFilename); | ||
unlink($tmpFilename); | ||
} | ||
return $ticket; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
class KRB5CCache { | ||
public function getEntries(): array { return [];} | ||
public function getName(): string {return "";} | ||
public function initKeytab(string $principal, string $keytab, array $flags = []): void {} | ||
public function initPassword(string $principal, string $password, array $flags = []): void {} | ||
public function isValid(): bool {return false;} | ||
public function open(string $source): void {} | ||
public function save(string $destination): void {} | ||
public function setConfig(string $destination): void {} | ||
} |