diff options
author | Vincent Petry <pvince81@owncloud.com> | 2014-03-27 12:18:51 +0100 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2014-03-27 12:18:51 +0100 |
commit | 1f6259d9c284fb917210fc438c9ff30a4549aed0 (patch) | |
tree | a83d4e72467f908ac4eca2294eea01fc7f5483f0 /apps/files_external/lib | |
parent | 1469ce7b38fbd55a64fad05e95ca124ababdf3df (diff) | |
parent | a1dca821f9c2cf770b6884e943a0841c0ab6171d (diff) | |
download | nextcloud-server-1f6259d9c284fb917210fc438c9ff30a4549aed0.tar.gz nextcloud-server-1f6259d9c284fb917210fc438c9ff30a4549aed0.zip |
Merge pull request #7875 from Xenopathic/files_external_smb_auto
SMB/CIFS mounts using ownCloud login, fixes #7843
Diffstat (limited to 'apps/files_external/lib')
-rwxr-xr-x | apps/files_external/lib/config.php | 26 | ||||
-rw-r--r-- | apps/files_external/lib/smb_oc.php | 93 |
2 files changed, 110 insertions, 9 deletions
diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php index f7caafb74aa..472c3963d51 100755 --- a/apps/files_external/lib/config.php +++ b/apps/files_external/lib/config.php @@ -5,6 +5,7 @@ * @author Michael Gapczynski * @copyright 2012 Michael Gapczynski mtgap@owncloud.com * @copyright 2014 Vincent Petry <pvince81@owncloud.com> +* @copyright 2014 Robin McCorkell <rmccorkell@karoshi.org.uk> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE @@ -122,11 +123,18 @@ class OC_Mount_Config { 'password' => '*Password', 'share' => 'Share', 'root' => '&Root')); + $backends['\OC\Files\Storage\SMB_OC'] = array( + 'backend' => 'SMB / CIFS using OC login', + 'configuration' => array( + 'host' => 'URL', + 'username_as_share' => '!Username as share', + 'share' => '&Share', + 'root' => '&Root')); } } if(OC_Mount_Config::checkcurl()){ - $backends['\OC\Files\Storage\DAV']=array( + $backends['\OC\Files\Storage\DAV']=array( 'backend' => 'WebDAV', 'configuration' => array( 'host' => 'URL', @@ -134,7 +142,7 @@ class OC_Mount_Config { 'password' => '*Password', 'root' => '&Root', 'secure' => '!Secure https://')); - $backends['\OC\Files\Storage\OwnCloud']=array( + $backends['\OC\Files\Storage\OwnCloud']=array( 'backend' => 'ownCloud', 'configuration' => array( 'host' => 'URL', @@ -185,7 +193,7 @@ class OC_Mount_Config { * @return array of mount point string as key, mountpoint config as value */ public static function getAbsoluteMountPoints($user) { - $mountPoints = array(); + $mountPoints = array(); $datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data"); $mount_file = \OC_Config::getValue("mount_file", $datadir . "/mount.json"); @@ -311,7 +319,7 @@ class OC_Mount_Config { 'backend' => $backends[$mount['class']]['backend'], 'options' => $mount['options'], 'applicable' => array('groups' => array($group), 'users' => array()), - 'status' => self::getBackendStatus($mount['class'], $mount['options']) + 'status' => self::getBackendStatus($mount['class'], $mount['options'], false) ); $hash = self::makeConfigHash($config); // If an existing config exists (with same class, mountpoint and options) @@ -341,7 +349,7 @@ class OC_Mount_Config { 'backend' => $backends[$mount['class']]['backend'], 'options' => $mount['options'], 'applicable' => array('groups' => array(), 'users' => array($user)), - 'status' => self::getBackendStatus($mount['class'], $mount['options']) + 'status' => self::getBackendStatus($mount['class'], $mount['options'], false) ); $hash = self::makeConfigHash($config); // If an existing config exists (with same class, mountpoint and options) @@ -381,7 +389,7 @@ class OC_Mount_Config { 'mountpoint' => substr($mountPoint, strlen($uid) + 8), 'backend' => $backends[$mount['class']]['backend'], 'options' => $mount['options'], - 'status' => self::getBackendStatus($mount['class'], $mount['options']) + 'status' => self::getBackendStatus($mount['class'], $mount['options'], true) ); } } @@ -394,7 +402,7 @@ class OC_Mount_Config { * @param array $options backend configuration options * @return bool true if the connection succeeded, false otherwise */ - private static function getBackendStatus($class, $options) { + private static function getBackendStatus($class, $options, $isPersonal) { if (self::$skipTest) { return true; } @@ -404,7 +412,7 @@ class OC_Mount_Config { if (class_exists($class)) { try { $storage = new $class($options); - return $storage->test(); + return $storage->test($isPersonal); } catch (Exception $exception) { \OCP\Util::logException('files_external', $exception); return false; @@ -471,7 +479,7 @@ class OC_Mount_Config { $mountPoints[$mountType] = $mount; } self::writeData($isPersonal, $mountPoints); - return self::getBackendStatus($class, $classOptions); + return self::getBackendStatus($class, $classOptions, $isPersonal); } /** diff --git a/apps/files_external/lib/smb_oc.php b/apps/files_external/lib/smb_oc.php new file mode 100644 index 00000000000..0c79c06c5df --- /dev/null +++ b/apps/files_external/lib/smb_oc.php @@ -0,0 +1,93 @@ +<?php +/** + * Copyright (c) 2014 Robin McCorkell <rmccorkell@karoshi.org.uk> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Files\Storage; + +require_once __DIR__ . '/../3rdparty/smb4php/smb.php'; + +class SMB_OC extends \OC\Files\Storage\SMB { + private $username_as_share; + + public function __construct($params) { + if (isset($params['host']) && \OC::$session->exists('smb-credentials')) { + $host=$params['host']; + $this->username_as_share = ($params['username_as_share'] === 'true'); + + $params_auth = \OC::$session->get('smb-credentials'); + $user = \OC::$session->get('loginname'); + $password = $params_auth['password']; + + $root=isset($params['root'])?$params['root']:'/'; + $share = ''; + + if ($this->username_as_share) { + $share = '/'.$user; + } elseif (isset($params['share'])) { + $share = $params['share']; + } else { + throw new \Exception(); + } + parent::__construct(array( + "user" => $user, + "password" => $password, + "host" => $host, + "share" => $share, + "root" => $root + )); + } else { + throw new \Exception(); + } + } + + public static function login( $params ) { + \OC::$session->set('smb-credentials', $params); + } + + public function isSharable($path) { + return false; + } + + public function test($isPersonal = true) { + if ($isPersonal) { + if ($this->stat('')) { + return true; + } + return false; + } else { + $smb = new \smb(); + $pu = $smb->parse_url($this->constructUrl('')); + + // Attempt to connect anonymously + $pu['user'] = ''; + $pu['pass'] = ''; + + // Share cannot be checked if dynamic + if ($this->username_as_share) { + if ($smb->look($pu)) { + return true; + } else { + return false; + } + } + if (!$pu['share']) { + return false; + } + + // The following error messages are expected due to anonymous login + $regexp = array( + '(NT_STATUS_ACCESS_DENIED)' => 'skip' + ) + $smb->getRegexp(); + + if ($smb->client("-d 0 " . escapeshellarg('//' . $pu['host'] . '/' . $pu['share']) . " -c exit", $pu, $regexp)) { + return true; + } else { + return false; + } + } + } +} |