Welcome to mirror list, hosted at ThFree Co, Russian Federation.

KerberosApacheAuth.php « src « smb « icewind « 3rdparty « files_external « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c49918be114a647148751cd3feec038185dd6d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
 * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
 *
 * @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\DependencyException;
use Icewind\SMB\Exception\Exception;

/**
 * Use existing kerberos ticket to authenticate and reuse the apache ticket cache (mod_auth_kerb)
 */
class KerberosApacheAuth extends KerberosAuth implements IAuth {
	/** @var string */
	private $ticketPath = "";

	/** @var bool */
	private $init = false;

	/** @var string|false */
	private $ticketName;

	public function __construct() {
		$this->ticketName = getenv("KRB5CCNAME");
	}


	/**
	 * Copy the ticket to a temporary location and use that ticket for authentication
	 *
	 * @return void
	 */
	public function copyTicket(): void {
		if (!$this->checkTicket()) {
			return;
		}
		$krb5 = new \KRB5CCache();
		$krb5->open($this->ticketName);
		$tmpFilename = tempnam("/tmp", "krb5cc_php_");
		$tmpCacheFile = "FILE:" . $tmpFilename;
		$krb5->save($tmpCacheFile);
		$this->ticketPath = $tmpFilename;
		$this->ticketName = $tmpCacheFile;
	}

	/**
	 * Pass the ticket to smbclient by memory instead of path
	 *
	 * @return void
	 */
	public function passTicketFromMemory(): void {
		if (!$this->checkTicket()) {
			return;
		}
		$krb5 = new \KRB5CCache();
		$krb5->open($this->ticketName);
		$this->ticketName = (string)$krb5->getName();
	}

	/**
	 * Check if a valid kerberos ticket is present
	 *
	 * @return bool
	 * @psalm-assert-if-true string $this->ticketName
	 */
	public function checkTicket(): bool {
		//read apache kerberos ticket cache
		if (!$this->ticketName) {
			return false;
		}

		$krb5 = new \KRB5CCache();
		$krb5->open($this->ticketName);
		/** @psalm-suppress MixedArgument */
		return count($krb5->getEntries()) > 0;
	}

	private function init(): void {
		if ($this->init) {
			return;
		}
		$this->init = true;
		// inspired by https://git.typo3.org/TYPO3CMS/Extensions/fal_cifs.git

		if (!extension_loaded("krb5")) {
			// https://pecl.php.net/package/krb5
			throw new DependencyException('Ensure php-krb5 is installed.');
		}

		//read apache kerberos ticket cache
		if (!$this->checkTicket()) {
			throw new Exception('No kerberos ticket cache environment variable (KRB5CCNAME) found.');
		}

		// note that even if the ticketname is the value we got from `getenv("KRB5CCNAME")` we still need to set the env variable ourselves
		// this is because `getenv` also reads the variables passed from the SAPI (apache-php) and we need to set the variable in the OS's env
		putenv("KRB5CCNAME=" . $this->ticketName);
	}

	public function getExtraCommandLineArguments(): string {
		$this->init();
		return parent::getExtraCommandLineArguments();
	}

	public function setExtraSmbClientOptions($smbClientState): void {
		$this->init();
		try {
			parent::setExtraSmbClientOptions($smbClientState);
		} catch (Exception $e) {
			// suppress
		}
	}

	public function __destruct() {
		if (!empty($this->ticketPath) && file_exists($this->ticketPath) && is_file($this->ticketPath)) {
			unlink($this->ticketPath);
		}
	}
}