SMB.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Valdnet <47037905+Valdnet@users.noreply.github.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_External\Lib\Backend;
  28. use Icewind\SMB\BasicAuth;
  29. use Icewind\SMB\KerberosApacheAuth;
  30. use Icewind\SMB\KerberosAuth;
  31. use OCA\Files_External\Lib\Auth\AuthMechanism;
  32. use OCA\Files_External\Lib\Auth\Password\Password;
  33. use OCA\Files_External\Lib\Auth\SMB\KerberosApacheAuth as KerberosApacheAuthMechanism;
  34. use OCA\Files_External\Lib\DefinitionParameter;
  35. use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  36. use OCA\Files_External\Lib\LegacyDependencyCheckPolyfill;
  37. use OCA\Files_External\Lib\StorageConfig;
  38. use OCP\IL10N;
  39. use OCP\IUser;
  40. class SMB extends Backend {
  41. use LegacyDependencyCheckPolyfill;
  42. public function __construct(IL10N $l, Password $legacyAuth) {
  43. $this
  44. ->setIdentifier('smb')
  45. ->addIdentifierAlias('\OC\Files\Storage\SMB')// legacy compat
  46. ->setStorageClass('\OCA\Files_External\Lib\Storage\SMB')
  47. ->setText($l->t('SMB/CIFS'))
  48. ->addParameters([
  49. new DefinitionParameter('host', $l->t('Host')),
  50. new DefinitionParameter('share', $l->t('Share')),
  51. (new DefinitionParameter('root', $l->t('Remote subfolder')))
  52. ->setFlag(DefinitionParameter::FLAG_OPTIONAL),
  53. (new DefinitionParameter('domain', $l->t('Domain')))
  54. ->setFlag(DefinitionParameter::FLAG_OPTIONAL),
  55. (new DefinitionParameter('show_hidden', $l->t('Show hidden files')))
  56. ->setType(DefinitionParameter::VALUE_BOOLEAN)
  57. ->setFlag(DefinitionParameter::FLAG_OPTIONAL),
  58. (new DefinitionParameter('case_sensitive', $l->t('Case sensitive file system')))
  59. ->setType(DefinitionParameter::VALUE_BOOLEAN)
  60. ->setFlag(DefinitionParameter::FLAG_OPTIONAL)
  61. ->setDefaultValue(true)
  62. ->setTooltip($l->t('Disabling it will allow to use a case insensitive file system, but comes with a performance penalty')),
  63. (new DefinitionParameter('check_acl', $l->t('Verify ACL access when listing files')))
  64. ->setType(DefinitionParameter::VALUE_BOOLEAN)
  65. ->setFlag(DefinitionParameter::FLAG_OPTIONAL)
  66. ->setTooltip($l->t("Check the ACL's of each file or folder inside a directory to filter out items where the account has no read permissions, comes with a performance penalty")),
  67. (new DefinitionParameter('timeout', $l->t('Timeout')))
  68. ->setType(DefinitionParameter::VALUE_HIDDEN)
  69. ->setFlag(DefinitionParameter::FLAG_OPTIONAL),
  70. ])
  71. ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD)
  72. ->addAuthScheme(AuthMechanism::SCHEME_SMB)
  73. ->setLegacyAuthMechanism($legacyAuth);
  74. }
  75. /**
  76. * @return void
  77. */
  78. public function manipulateStorageConfig(StorageConfig &$storage, ?IUser $user = null) {
  79. $auth = $storage->getAuthMechanism();
  80. if ($auth->getScheme() === AuthMechanism::SCHEME_PASSWORD) {
  81. if (!is_string($storage->getBackendOption('user')) || !is_string($storage->getBackendOption('password'))) {
  82. throw new \InvalidArgumentException('user or password is not set');
  83. }
  84. $smbAuth = new BasicAuth(
  85. $storage->getBackendOption('user'),
  86. $storage->getBackendOption('domain'),
  87. $storage->getBackendOption('password')
  88. );
  89. } else {
  90. switch ($auth->getIdentifier()) {
  91. case 'smb::kerberos':
  92. $smbAuth = new KerberosAuth();
  93. break;
  94. case 'smb::kerberosapache':
  95. if (!$auth instanceof KerberosApacheAuthMechanism) {
  96. throw new \InvalidArgumentException('invalid authentication backend');
  97. }
  98. $credentialsStore = $auth->getCredentialsStore();
  99. $kerbAuth = new KerberosApacheAuth();
  100. // check if a kerberos ticket is available, else fallback to session credentials
  101. if ($kerbAuth->checkTicket()) {
  102. $smbAuth = $kerbAuth;
  103. } else {
  104. try {
  105. $credentials = $credentialsStore->getLoginCredentials();
  106. $user = $credentials->getLoginName();
  107. $pass = $credentials->getPassword();
  108. preg_match('/(.*)@(.*)/', $user, $matches);
  109. $realm = $storage->getBackendOption('default_realm');
  110. if (empty($realm)) {
  111. $realm = 'WORKGROUP';
  112. }
  113. if (count($matches) === 0) {
  114. $username = $user;
  115. $workgroup = $realm;
  116. } else {
  117. $username = $matches[1];
  118. $workgroup = $matches[2];
  119. }
  120. $smbAuth = new BasicAuth(
  121. $username,
  122. $workgroup,
  123. $pass
  124. );
  125. } catch (\Exception $e) {
  126. throw new InsufficientDataForMeaningfulAnswerException('No session credentials saved');
  127. }
  128. }
  129. break;
  130. default:
  131. throw new \InvalidArgumentException('unknown authentication backend');
  132. }
  133. }
  134. $storage->setBackendOption('auth', $smbAuth);
  135. }
  136. }