smb_oc.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Files\Storage;
  26. use Icewind\SMB\Exception\AccessDeniedException;
  27. use Icewind\SMB\Exception\Exception;
  28. use Icewind\SMB\Server;
  29. class SMB_OC extends SMB {
  30. private $username_as_share;
  31. /**
  32. * @param array $params
  33. * @throws \Exception
  34. */
  35. public function __construct($params) {
  36. if (isset($params['host'])) {
  37. $host = $params['host'];
  38. $this->username_as_share = ($params['username_as_share'] === 'true');
  39. // dummy credentials, unused, to satisfy constructor
  40. $user = 'foo';
  41. $password = 'bar';
  42. if (\OC::$server->getSession()->exists('smb-credentials')) {
  43. $params_auth = json_decode(\OC::$server->getCrypto()->decrypt(\OC::$server->getSession()->get('smb-credentials')), true);
  44. $user = \OC::$server->getSession()->get('loginname');
  45. $password = $params_auth['password'];
  46. } else {
  47. // assume we are testing from the admin section
  48. }
  49. $root = isset($params['root']) ? $params['root'] : '/';
  50. $share = '';
  51. if ($this->username_as_share) {
  52. $share = '/' . $user;
  53. } elseif (isset($params['share'])) {
  54. $share = $params['share'];
  55. } else {
  56. throw new \Exception();
  57. }
  58. parent::__construct(array(
  59. "user" => $user,
  60. "password" => $password,
  61. "host" => $host,
  62. "share" => $share,
  63. "root" => $root
  64. ));
  65. } else {
  66. throw new \Exception();
  67. }
  68. }
  69. /**
  70. * Intercepts the user credentials on login and stores them
  71. * encrypted inside the session if SMB_OC storage is enabled.
  72. * @param array $params
  73. */
  74. public static function login($params) {
  75. $mountpoints = \OC_Mount_Config::getAbsoluteMountPoints($params['uid']);
  76. $mountpointClasses = array();
  77. foreach($mountpoints as $mountpoint) {
  78. $mountpointClasses[$mountpoint['class']] = true;
  79. }
  80. if(isset($mountpointClasses['\OC\Files\Storage\SMB_OC'])) {
  81. \OC::$server->getSession()->set('smb-credentials', \OC::$server->getCrypto()->encrypt(json_encode($params)));
  82. }
  83. }
  84. /**
  85. * @param string $path
  86. * @return boolean
  87. */
  88. public function isSharable($path) {
  89. return false;
  90. }
  91. /**
  92. * @param bool $isPersonal
  93. * @return bool
  94. */
  95. public function test($isPersonal = true) {
  96. if ($isPersonal) {
  97. if ($this->stat('')) {
  98. return true;
  99. }
  100. return false;
  101. } else {
  102. $server = new Server($this->server->getHost(), '', '');
  103. try {
  104. $server->listShares();
  105. return true;
  106. } catch (AccessDeniedException $e) {
  107. // expected due to anonymous login
  108. return true;
  109. } catch (Exception $e) {
  110. return false;
  111. }
  112. }
  113. }
  114. }